This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Is there a difference between these 2 ways to use thread ? if so, which one is the best practice
1 - Instantiating one runnable and passing it to all the threads during their creation : MyRunnable r1 = new MyRunnable(); Thread th1 = new Thread(r1); Thread th2 = new Thread(r1); Thread th3 = new Thread(r1);
2 - Instantiating a runnable in every Thread creation : Thread th1 = new Thread(new MyRunnable()); Thread th2 = new Thread(new MyRunnable()); Thread th3 = new Thread(new MyRunnable());
SCJP 5 (76%)
SCWCD 5 (86%)
"The greatest glory in living lies not in never falling, but in raising every time we fall.".. Nelson Mandela
There are difference in these two type of creating threads in first case all thread will be using the same target(ie. same runnable,sanme run method ) as you have passed the same runnable to all threds.Thus their will be 3 threads to for the same target.
in second case each thread is cfreated by it's own target ,hence each thread can have it's own run method.
i think now you should get it. if require further clarification then ask.
It's not a matter of implementing Runnable vs extending Thread. It's a question of using one same object runnable while instantiating several threads, or instatiating several threads each one with its proper runnable object.
Mamadou is right, this is not about the convenience of extending Thread or implementing Runnable.
This is about several threads sharing the same Runnable object or not.
As Wim well said before, there is not a approach better than the other. They are simply different. And if you understand it well, you will realize they both can be used in different situations.
Joined: Mar 15, 2008
sorry i completly gave answer for different question well....... test r1=new test() Thread t1=new Thread(r1); Thread t2=new Thread(r1); Thread t3=new Thread(r1); t1.start(); t2.start(); t3.start();
new Thread(new test()).start(); new Thread(new test()).start(); new Thread(new test()).start();
In first case all the Threads Doing on one Single object.If that object has synchronized method then only one thread can access at a time. but in the seccond case you create three different objects so no thread is wait .
If you are NOT using synchronize(this) in your run() method then it should not matter as far as thread executions go, the only difference would be that you create 2 unnecessary objects in one case VS the other.
If you are using synchronized(this) in your run() method implementation then, if you have only one instance of MyRunnable, the threads will actually execute the code in the run() method one at a time; and again, if you have 3 different instances of MyRunnable - the result will be that the threads will execute simultaneously.
Depends on what you are trying to achieve. I would preffer to use only one instance of MyRunnable as much as possible.
I think Dumitru is right. Its like three people want to drink water. By making one Runnable object and passing it to three threads is equivalent to all three guys are using same bottle to drink water, in second case each one is having different bottle to drink water. which way you prefer depends upon the circumstances..!
"vasu", Please check your private messages regarding an important administrative matter. -Ben
Joined: Dec 27, 2007
Thanks for all your responses,
In the shop I work, I saw a piece of java code that uses serveral runnable instances to create several threads (instead of all threads sharing the same one runnable), and in the run() method, the programmmer uses synchronization, but I think that's not necessary given the explanations you gave me . Am I right that we don't need Synchronization in this particular case ?