Determining the order of threads

Q.--" I created five(5) thread instances,say "1","2","3","4","5" and start all of them at the same time,one after the other. The priorities for the threads are same.what should be done so that the threads run concurrently in the same order,i.e1,2,3,4,5(Is there any way at all)"?

Questions by Sumantra_07

Showing Answers 1 - 5 of 5 Answers

xyz

  • Nov 27th, 2012
 

Code
  1. span style="font-style: italic;">// TODO Auto-generated catch block

  2. // t1.setName("Thread 1");

  3. // TODO Auto-generated catch block

  4. // TODO Auto-generated catch block

  5. //  t3.setName("Thread 3");

  6. // TODO Auto-generated catch block

  7. //  t4.setName("Thread 4");

  8.   t4.setPriority(2);

  9.  

  10.   t1.start();

  11.   t2.start();

  12.   t3.start();

  13.   t4.start();

  14. }

  15.  

  16. }

  Was this answer useful?  Yes

Amit

  • Dec 7th, 2012
 

You can use JOIN() method to execute them one after another.
e.g :- t1.start();
t1.join();
t2.start();
t2.join();

  Was this answer useful?  Yes

Praveen Kumar

  • Dec 13th, 2012
 

There is no way to determine which thread is executed first, it is solely dependent on the scheduling algorithms adapted by the operating system and underlying hardware architecture, one way to guarantee the that the thread runs in a particular order is using synchronized methods.

  Was this answer useful?  Yes

mduhan

  • Apr 8th, 2013
 

We have to implement run method to create a thread.We need to make that method synchronized so that only one thread can go inside and control will be given to second only and only of first is completed.

  Was this answer useful?  Yes

Vijay

  • Jan 10th, 2014
 

We can do that by using the join. In the second thread run method, we can call the join on first thread object. In the third thread run method, we can call the join on second thread object. Like this way, we can make sure first thread will execute before second and secondll execute before third thread.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions