Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Please post a small, complete program that compiles, executes and shows the problem.
If you don't understand my answer, don't ignore it, ask a question.
In fact, this already is a small, complete program. Just wrap the code in the main method and that's it.
Please post a complete program that compiles, executes and shows the problem. It is important that I have exactly the same code that you are executing so that I can see what the code does when it executes. If I change it, it could be different.
If you don't understand my answer, don't ignore it, ask a question.
OK, here's the full code:
The problem is that "I was called!" appears only 2 times on the screen. Instead of 10.
Look at the output from this code:The use of t the first time is with the override of run()public class Thread_Program { public static void main(String[] args) throws InterruptedException { Thread t = new Thread(){ public void run(){ System.out.println("I was called! threadname=" + Thread.currentThread().getName()); } }; for(int i = 0; i < 10; i++){ System.out.print(i + " created t= " + t.getName() + " t.run() on next line "); t.run(); // to print msg t.start(); t.join(); t = new Thread(t); } } }
for the rest of the times, the run() method is the empty default from the Thread class.
If you don't understand my answer, don't ignore it, ask a question.
But if I pass a Runnable to a Thread constructor, the Runnable's run() method will be called on thread's start. Thread implements Runnable. Hence, when I say t = new Thread(t) the new thread must run the run method of 't' (who is in the constructor).
The first time. The second time t is different.new thread must run the run method of 't' (who is in the constructor).
Look at the output from the code I posted.
If you don't understand my answer, don't ignore it, ask a question.
Watch your code run in a debugger where you can see what happens. That is probably the best way to "get it" with the code you posted.
Norm, jps, thank you very much! Now I see it. Actually, it becomes very, very obvious in a debugger. So in the first iteration we had a direct call to the target run() method. At the end of this iteration this thread is passed to the standard system thread constructor as a Runnable. In the 2nd iteration the run() on 't' causes the run() on a thread from 1st iteration. In 3rd iteration call to run() on 't' also causes a call to a thread from a previous iteration, and it is a standard system thread with an empty run().
There is no such thing as a parent-child relationship between threads in Java. Once created, they have a life of their own.
Regarding performance, you may want to use an ExecutorService to control the number of threads created in your application. Too many threads will kill performance for sure. See the Executors class too. I sure they will help you....
The way you are creating threads is perfectly ok if it is only a few. Otherwise, executor services are the preferred method.
Last edited by copeg; May 29th, 2013 at 09:31 AM. Reason: Removed link
a) Technically, there is such a thing via the ThreadGroup class b) what does this have to do with the original posters question? Please stop posting links to spurious non-java doc, non-oracle, 3rd party websites - doing so as a new user, resurrecting several week old posts makes it look very much like self promotion - aka spam. Continue to do so and further action will be taken.There is no such thing as a parent-child relationship between threads