• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Doubt  RSS feed

 
pras
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?
 
Rob Spoor
Sheriff
Posts: 21048
85
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.
 
Bob Ruth
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!