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 - 15 of 15 Answers

xyz

  • Nov 27th, 2012
 

Code
  1.  class test extends Thread

  2. {

  3.   public static void main(String[] args)

  4. {

  5.  

  6.  

  7.   Thread t1 =  new test() {public void  run()

  8.   {

  9.          

  10.          for (int i=0; i <=10;i++){

  11.                  System.out.println(this.getName());

  12.                  try {

  13.                         Thread.sleep(100);

  14.                 } catch (InterruptedException e) {

  15.                         // TODO Auto-generated catch block

  16.                         e.printStackTrace();

  17.                 }

  18.                

  19.          }

  20.   }};

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

  22.   t1.setPriority(2);

  23.  

  24.   Thread t2 =  new test(){public void  run()

  25.   {

  26.          

  27.          for (int i=0; i <=10;i++)

  28.          {

  29.                  System.out.println(this.getName());

  30.                  try {

  31.                                 Thread.sleep(100);

  32.                         } catch (InterruptedException e) {

  33.                                 // TODO Auto-generated catch block

  34.                                 e.printStackTrace();

  35.                         }

  36.          }

  37.   }};

  38.  

  39.   t2.setPriority(2);

  40.  

  41.   Thread t3 =  new test(){public void  run()

  42.   {

  43.          

  44.          for (int i=0; i <=10;i++)

  45.          {

  46.                  System.out.println(this.getName());

  47.                  try {

  48.                                 Thread.sleep(100);

  49.                         } catch (InterruptedException e) {

  50.                                 // TODO Auto-generated catch block

  51.                                 e.printStackTrace();

  52.                         }

  53.          }

  54.   }};

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

  56.   t2.setPriority(2);

  57.  

  58.   Thread t4 =  new test(){public void  run()

  59.   {

  60.          

  61.          for (int i=0; i <=10;i++)

  62.          {

  63.                  System.out.println(this.getName());

  64.                  try {

  65.                                 Thread.sleep(100);

  66.                         } catch (InterruptedException e) {

  67.                                 // TODO Auto-generated catch block

  68.                                 e.printStackTrace();

  69.                         }

  70.          }

  71.   }};

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

  73.   t4.setPriority(2);

  74.  

  75.   t1.start();

  76.   t2.start();

  77.   t3.start();

  78.   t4.start();

  79. }

  80.  

  81. }

  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