Wednesday, July 17, 2019

How to Run Threads in Sequence In Java

Q- How to Run Threads in Sequence? Or How to Run Threads in Sequence without join? Or How to make sure sequence of threads in java? 

Example - 1 : Using Join() method.

public class ThreadSequenceUsingJoin {

 public static void main(String[] args) {
  Worker worker = new Worker();
  // Three threads
  Thread t1 = new Thread(worker);
  Thread t2 = new Thread(worker);
  Thread t3 = new Thread(worker);

  try {
   // First thread
   t1.start();
   t1.join();
   // Second thread
   t2.start();
   t2.join();
   // Third thread
   t3.start();
   t3.join();
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

class Worker implements Runnable {

 @Override
 public void run() {
  System.out.println("In run method " + Thread.currentThread().getName());
 }
}
Output

In run method Thread-0
In run method Thread-1
In run method Thread-2
Example - 2 :Without using Join() method.

public class Thread1 extends Thread {

 ThreadController threadControler;
 int sequence;

 public Thread1(ThreadController threadControler, int sequence) {
  this.threadControler = threadControler;
  this.sequence = sequence;
 }

 @Override
 public void run() {
  while (true) {
   synchronized (threadControler) {
    if (threadControler.getCount() != sequence) {

     /*
      * System.out.println("In thred --- Thread1 : Count is " +
      * threadControler.getCount() + ", Sequence is " + sequence);
      */
     try {
      threadControler.wait();
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    } else if (threadControler.getCount() == sequence) {
     System.out.println(
       "Print :: Thread1, Count is " + threadControler.getCount() + ", Sequence is " + sequence);
     threadControler.incrementCount();
     threadControler.notifyAll();
    }
   }
  }
 }
}

public class Thread2 extends Thread {

 ThreadController threadControler;
 int sequence;

 public Thread2(ThreadController threadControler, int sequence) {
  this.threadControler = threadControler;
  this.sequence = sequence;
 }

 @Override
 public void run() {
  while (true) {
   synchronized (threadControler) {
    if (threadControler.getCount() != sequence) {
     /*
      * System.out.println("In thred --- Thread2 : Count is " +
      * threadControler.getCount() + ", Sequence is " + sequence);
      */
     try {
      threadControler.wait();
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    } else if (threadControler.getCount() == sequence) {
     System.out.println(
       "Print :: Thread2, Count is " + threadControler.getCount() + ", Sequence is " + sequence);
     threadControler.incrementCount();
     threadControler.notifyAll();
    }
   }
  }
 }

}

public class Thread3 extends Thread {

 ThreadController threadControler;
 int sequence;

 public Thread3(ThreadController threadControler, int sequence) {
  this.threadControler = threadControler;
  this.sequence = sequence;
 }

 @Override
 public void run() {
  while (true) {
   synchronized (threadControler) {
    if (threadControler.getCount() != sequence) {
     /*
      * System.out.println("In thred --- Thread3 : Count is " +
      * threadControler.getCount() + ", Sequence is " + sequence);
      */
     try {
      threadControler.wait();
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    } else if (threadControler.getCount() == sequence) {
     System.out.println(
       "Print :: Thread3, Count is " + threadControler.getCount() + ", Sequence is " + sequence);
     threadControler.incrementCount();
     threadControler.notifyAll();
    }
   }
  }
 }
}

public class ThreadController {

 private volatile int count = 1;

 public int getCount() {
  return count;
 }

 public void incrementCount() {
  count++;
 }
}
Now test the above code.

public class ThreadSequenceTest {
 public static void main(String[] args) {

  // In this code we can change sequence number. So we can get the output in same
  // sequence. as in below code i want to print t2 than t1 then t3
  ThreadController threadControler = new ThreadController();
  Thread1 t1 = new Thread1(threadControler, 2);
  Thread2 t2 = new Thread2(threadControler, 1);
  Thread3 t3 = new Thread3(threadControler, 3);

  t1.start();
  t2.start();
  t3.start();
 }
}
Output

Print :: Thread2, Count is 1, Sequence is 1
Print :: Thread1, Count is 2, Sequence is 2
Print :: Thread3, Count is 3, Sequence is 3

No comments:

Post a Comment