• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Thread Doubt  RSS feed

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class MyRunnable implements Runnable {
public void run() {
S.O.P("important job running");
}
}
class TestThreads {
public static void main(String args[]) {
MyRunnable r=new MyRunnable();
Thread foo=new Thread(r);
Thread bar=new Thread(r);
}
}
/************* DOUBT**********************/
now assume that both threads are started ie foo and bar
so now is the job executed 2 times since there are 2 threads we created or do the 2 threads Share the workload and execute the job only once?
 
Sheriff
Posts: 21459
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each thread will call the run() method of your runnable, so the code will be executed twice.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it ran two times but each runs in it's own thread.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!