Create Thread...

Discussion in 'Java' started by yffaria, May 12, 2006.

  1. yffaria

    yffaria Guest

    Its possible create a class extended by thread and in this class, to
    instance other class extended by thread too?

    What problems I found when I do that?

    How to create more functions of thread class in class extended by
    threads?

    Thx!
     
    yffaria, May 12, 2006
    #1
    1. Advertising

  2. yffaria wrote:
    > Its possible create a class extended by thread and in this class, to
    > instance other class extended by thread too?
    >
    > What problems I found when I do that?
    >
    > How to create more functions of thread class in class extended by
    > threads?


    You can't insert a class into the inheritance hierarchy after the fact.
    You could do that in Ruby though...

    You can either implement Runnable and let all other Runnables inherit
    that or inherit Thread and make your thread classes inherit that.

    Regards

    robert
     
    Robert Klemme, May 12, 2006
    #2
    1. Advertising

  3. yffaria

    Shin Guest

    yffaria wrote:
    > Its possible create a class extended by thread and in this class, to
    > instance other class extended by thread too?
    >
    > What problems I found when I do that?
    >
    > How to create more functions of thread class in class extended by
    > threads?


    I'd like to see the context of this question; it might be interesting.
    But in Java language, you can not insert a layer in between exsiting
    class hierarchy; with some tools, though, you can do that

    -Shin
     
    Shin, May 12, 2006
    #3
  4. yffaria

    yffaria Guest

    The context is: I have one class (not thread class), and in this class
    I have two class extended by thread. In one of the thread class, I have
    on method that I open one file and write something, and this is do by
    the other class thread. I need do this by one thread. So when I
    initialize my other thread all happen normaly, but after when I start
    the thread, nothing happen.

    Are you understand me?
     
    yffaria, May 13, 2006
    #4
  5. "yffaria" <> wrote in message
    news:...
    > The context is: I have one class (not thread class), and in this class
    > I have two class extended by thread. In one of the thread class, I have
    > on method that I open one file and write something, and this is do by
    > the other class thread. I need do this by one thread. So when I
    > initialize my other thread all happen normaly, but after when I start
    > the thread, nothing happen.
    >
    > Are you understand me?


    It's definately a challenge for me. You say that you have a class extended
    by Thread, which means (to me, at least) that you have somehow made the
    system java.lang.Thread class extend (subclass) your class. This is
    extremely unlikely. I am more likely to believe there is a language problem
    and that you really have a class that contains two objects whose classes
    extend (are subclasses of) Thread. One thread opens a file and writes.

    Overally, I'm still really not sure what you're asking. My questions to you
    (to help you better) are:

    Do you mean extends and not extended by?
    Do you really need to extend Thread at all instead of supplying a Runnable?
    Why do you think you need to have a thread that starts other threads?
    What do you really want these threads to do?

    Cheers,
    Matt Humphrey http://www.iviz.com/
     
    Matt Humphrey, May 13, 2006
    #5
  6. yffaria

    Guest

    My File.java is:

    class File{
    public File(int poolSize){
    //start poolSize threads of the PoolThreads
    }

    private final class PoolThreads extends Thread{
    public void run(){
    ...
    exec(some parameters);
    ...
    }
    private int exec(some parameters){
    ...
    new ProcThread().start();
    ...
    }
    }

    public class ProcThread extends Threads{
    ...
    }
    }

    What was wrong with him?
     
    , May 15, 2006
    #6
  7. <> wrote in message
    news:...
    > My File.java is:
    >
    > class File{
    > public File(int poolSize){
    > //start poolSize threads of the PoolThreads
    > }
    >
    > private final class PoolThreads extends Thread{
    > public void run(){
    > ...
    > exec(some parameters);
    > ...
    > }
    > private int exec(some parameters){
    > ...
    > new ProcThread().start();
    > ...
    > }
    > }
    >
    > public class ProcThread extends Threads{
    > ...
    > }
    > }
    >
    > What was wrong with him?


    Well, this answers the first question in that you have classes that extend
    Thread. It certainly is possible to have a thread that instantiates
    another thread. This brings us to the question of whether doing so solves
    the problem. I guess you are trying to make a thread pool, but it would
    really help if you could say what you want this to do. A thread pool is not
    a kind of thread itself, so it is probably not meaningful to have it extend
    Thread. In addition, there are some well-known techniques for this--see
    these links.

    http://www.informit.com/articles/article.asp?p=30483&rl=1

    http://www-128.ibm.com/developerworks/library/j-jtp0730.html

    Also the latest versions (1.5) of Java appear to include a ThreadPool
    system. I havn't used these, but look for "ThreadPoolExecutor" and you will
    probably save yourself a huge amount of trouble.

    Cheers,
    Matt Humphrey http://www.iviz.com/
     
    Matt Humphrey, May 16, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman

    Terminating a thread from the main thread

    Charles A. Lackman, Dec 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,548
    Herfried K. Wagner [MVP]
    Dec 9, 2004
  2. pawo
    Replies:
    0
    Views:
    502
  3. Stephen Miller
    Replies:
    3
    Views:
    3,977
    Stephen Miller
    Jul 2, 2004
  4. Johanna
    Replies:
    0
    Views:
    608
    Johanna
    Oct 13, 2004
  5. Lialie - KingMax
    Replies:
    1
    Views:
    302
    John Henry
    Dec 15, 2006
Loading...

Share This Page