This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread question" Watch "Thread question" New topic
Author

Thread question

Mamadou Touré
Ranch Hand

Joined: Dec 27, 2007
Posts: 189
Hi everyone,

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);

or

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());

Regards


SCJP 5 (76%)
SCWCD 5 (86%)
SCBCD 5(70%)
--------------------
"The greatest glory in living lies not in never falling, but in raising every time we fall.".. Nelson Mandela
ishmayel vemuru
Ranch Hand

Joined: Jun 13, 2007
Posts: 45
I think there is no difference.
ishmayel.
Tarun Kumar
Greenhorn

Joined: Apr 30, 2008
Posts: 26
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.


Tarun Kumar
Mamadou Touré
Ranch Hand

Joined: Dec 27, 2007
Posts: 189
But is one way better than the other ??
Wim Molenberghs
Greenhorn

Joined: Jan 31, 2008
Posts: 16
Originally posted by Mamadou Tour�:
But is one way better than the other ??


The one is not better or worser than the other because they are very different.

try to run the following code with the 2 diffent threads and tell us what you see


[ April 30, 2008: Message edited by: Wim Molenberghs ]
vasu chowdary
Ranch Hand

Joined: Mar 15, 2008
Posts: 88
by implementing Runnable interface is better..because if you want to extend antoher class rather than Thread what you can do.(there is no multiple inheritence in java).other than this both are same.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39575
    
  27
There's also a FAQ page about this: ExtendingThreadVsImplementingRunnable


Ping & DNS - updated with new look and Ping home screen widget
Mamadou Touré
Ranch Hand

Joined: Dec 27, 2007
Posts: 189
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.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
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.
vasu chowdary
Ranch Hand

Joined: Mar 15, 2008
Posts: 88
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();


vs

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 .
Dumitru Postoronca
Ranch Hand

Joined: May 06, 2008
Posts: 46
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.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"vasu",
Please check your private messages regarding an important administrative matter.
-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

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..!


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"vasu",
Please check your private messages regarding an important administrative matter.
-Ben
Mamadou Touré
Ranch Hand

Joined: Dec 27, 2007
Posts: 189
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 ?

Regards
[ May 06, 2008: Message edited by: Mamadou Tour� ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread question
 
Similar Threads
Thread Interruption
Exeptions with Threads
why thread priority is not working here
How to interrupt or stop the threads