GeekInterview.com
Series: Subject: Topic:
Question: 11 of 573

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)"?
Asked by: Sumantra_07 | Member Since Jun-2012 | Asked on: Jun 27th, 2012

View all questions by Sumantra_07

Showing Answers 1 - 4 of 4 Answers
xyz

Answered On : 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. }

  
Login to rate this answer.
Amit

Answered On : Dec 7th, 2012

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

  
Login to rate this answer.
Praveen Kumar

Answered On : 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.

  
Login to rate this answer.
mduhan

Answered On : Apr 8th, 2013

View all answers by mduhan

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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.