Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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: 21464
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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!