• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Thread question

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there is no difference.
ishmayel.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mamadou Touré
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But is one way better than the other ??
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's also a FAQ page about this: ExtendingThreadVsImplementingRunnable
 
Mamadou Touré
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"vasu",
Please check your private messages regarding an important administrative matter.
-Ben
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"vasu",
Please check your private messages regarding an important administrative matter.
-Ben
 
Mamadou Touré
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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� ]
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic