Preventing Thread from execution

Preventing Thread from execution

We can prevent a thread from execution by using the following methods.

  1. yield()
  2. join()
  3. sleep()
yield()

The thread which is called yield() method temporarily pause the execution to give the chance for remaining threads of same priority. If there is no waiting thread or all waiting threads having low priority. Then the same thread will get the chance immediately for the execution.

class MyThread extends Thread {
   public void run() {
      for (int i = 0; i < 5; i++) {
         System.out.println("Child Thread");
         Thread.yield();
      }
   }
}

public class MyThreadYield {
   public static void main(String arg[]) {
      MyThread t = new MyThread();
      t.start();
      for (int i = 0; i < 5; i++) {
         System.out.println("Main Thread");
      }
   }
}

Output

Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread

In this case main thread will get chance more no of times for execution. Because child thread intentionally calling β€œyield()” method. As the yield method is native method some Operating system may not provide the support for this.

join()

If a thread wants to wait until some other thread completion then we should go for join method.

E.g

If a thread t1 executes t2.join(), then t1 will be entered into waiting state until t2 completion.

public final void join() throws InterruptedException 
public final void join(long ms) throws InterruptedException 
public final void join(long ms, int ns) throws InterruptedException

E.g

class MyThread extends Thread {
   public void run() {
      for (int i = 0; i < 5; i++) {
         System.out.println("Child Thread");
         try {
            Thread.sleep(1000);
         } catch(InterruptedException ie) { }
      }
   }
}

public class MyThreadJoin {
   public static void main(String arg[]) throws InterruptedException {
      MyThread t = new MyThread();
      t.start();
      t.join();
      for (int i = 0; i < 5; i++) {
         System.out.println("Main Thread");
      }
   }
}

Output

Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread

If we are commenting t.join() then both Threads will be executed simultaneously and we can’t expect exact execution order.

If we are not commenting t.join() then main Thread will wait until completing child Thread in this the output is Child Thread 5 times followed by Main Thread 5 times.

Waiting of child Thread until completing main Thread
class MyThread extends Thread {
   static Thread mt;
   public void run() {
      try {
            mt.join();
         } catch(InterruptedException ie) { }
      for (int i = 0; i < 5; i++) {
         System.out.println("Child Thread");
      }
   }
}

public class MyThreadJoin {
   public static void main(String arg[]) throws InterruptedException {
      MyThread t = new MyThread();
      t.start();
      for (int i = 0; i < 5; i++) {
         System.out.println("Main Thread");
      }
   }
}

Output

Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread

Note

If main thread calls join() on child thread object and child thread called join() on main thread object then both threads will wait for each other forever and the program will be hanged(like deadlock if a Thread class join() method on the same thread itself then the program will be hanged).

sleep()

If a method has to wait some predefined amount of time with out execution then we should go for sleep() method.

public static native void sleep(long ms) throws InterruptedException 
public static void sleep(long ms,int ns) throws InterruptedException 
class MyThread extends Thread {
   public void run() {
      try {
         for (int i = 0; i < 5; i++) {
            System.out.println("This is Lazy Method");
            Thread.sleep(1000);
         }
      } catch (InterruptedException e) {
         System.out.println(e);
      }
   }
}

public class MyThreadSleep {
   public static void main(String arg[]) throws InterruptedException {
      MyThread t = new MyThread();
      t.start();
      System.out.println("Main Thread");
   }
}

Output

Main Thread
This is Lazy Method
This is Lazy Method
This is Lazy Method
This is Lazy Method
This is Lazy Method

Preventing Thread from execution


Scroll to top