Hi i'm pretty new to the whole java thing with basic skills in the non graphical parts of java and some limited swing skills. There's this thing about threads that's sorta irritating me. I would like to know.....if this is possible....how i can run multiple threads in the same class environment, each of which does something different (ie. has its own run() method). I always implement Runnable because its easier for me so if anyone knows how to do this I would be most appreciative. I have a suspicion that you can't have more than one run() in a given class but if you can, that's what i'd like to do. Thanks :)

as an edit to the above post, I am inquiring as to how to do something like the following:

Thread runner1 = new Thread(???)
Thread runner2 = new Thread(???)
Thread runner3 = new Thread(???)
runner1.start;
runner2.start;
runner3.start;


run() {
...
}


run() {
...
}

run() {
...
}

You can use anonymous inner classes. Hardcore Java book has good reasoning for when you should or should not use anonymous inner classes.

For more information about nested inner classes, read Ch 6 of Hardcore Java
http://www.onjava.com/pub/a/onjava/excerpt/HardcoreJava_chap06/index.html
http://www.onjava.com/pub/a/onjava/excerpt/HardcoreJava_chap06/index1.html
http://www.onjava.com/pub/a/onjava/excerpt/HardcoreJava_chap06/index2.html

public void init() {
   Thread r1 = (new Thread() {
      public void run() {
         ....
      }
   });
    Thread r2 = (new Thread() {
      public void run() {
         ....
      }
   });
   Thread r3 = (new Thread() {
      public void run() {
         ....
      }
   });
   r1.start(); r2.start(); r3.start();
}

Hope this helps!


Ed

Thanks for the reply....so what you're saying is that I must make sort of subclasses inside my main class in order to isolate the run methods for each thread?....are there any example of code that do this? (goes to check provided links)

These aren't really subclasses. These are anonymous classes that can exist almost like variables in your code. Notice that I put the statements within a function. Isn't my code snippet example code already?


Ed

Thanks for the reply....so what you're saying is that I must make sort of subclasses inside my main class in order to isolate the run methods for each thread?....are there any example of code that do this? (goes to check provided links)

By the way... you may not necessarily find much example code to embed the threads as anonymous classes because it's not good programming practice to write code like that. Read the chapter links of Hardcore Java that I sent you. They talk in detail about it all.


Ed

I see...I imagine it would be easier and perhaps neater to make new classes for each thread with a run method for each and call instances of these classes from the first class. The method you describe makes sense to me but the mechanics of actually writing the code as described in the links you provided are confusing to me so I think if it is possible I may opt to just make a new class for each thread. But i thank you for the reference material anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.