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

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

yield() method

public static native void yield();

  1. The thread which is called yield() method temporarily pause the execution to give the chance for remaining threads of same priority.
  2. 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.

Thread.yield

Example:


class TestThread extends Thread{
    public void run() {
        for (int i = 1; i <= 5; i++) {
            System.out.println("Child thread");
            //calling yield() method
            Thread.yield();
        }
    }
}

public class ThreadDemo {
	 
    public static void main(String[] args) {
         
        TestThread testThread = new TestThread();
        testThread.start();
        for (int i = 1; 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 the above program, main thread will get chance more no of times for execution, Because child thread intentionally calling “yield()” method explicitly. As the yield method is native method,some Operating system may not provide the support for this.

join() method

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

  1. public final void join() throws InterruptedException
  2. public final void join(long ms) throws InterruptedException
  3. public final void join(long ms, int ns) throws InterruptedException

Thread.join

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


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

public class ThreadDemo {
	 
    public static void main(String[] args) throws InterruptedException {
 
        TestThread testThread = new TestThread();
        testThread.start();
        testThread.join();//------(1)
        for (int i = 1; i <= 5; i++) {
            System.out.println("Main thread");
        }
    }
 
}

Note:

  1. If we are commenting line 1,then both threads will execute simultaneously and we can’t except exact output.
  2. If we are not commenting line 1,then main thread will execute join() method on child thread object.Hence main thread will enter into the waiting state until child thread completion,Hence the following is the output.
  3. Child thread
    Child thread
    Child thread
    Child thread
    Child thread
    Main thread
    Main thread
    Main thread
    Main thread
    Main thread
    
  4. If a thread calls join method on the same thread object,then that thread entered into infinite waiting

sleep() method

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

  1. public static void sleep(long ms)throws InterruptedException
  2. public static void sleep(long m, int m)throws InterruptedException

Thread.sleep

class TestThread extends Thread {
    public void run() {
        for (int i = 1; i <= 5; i++) {
            try {
                Thread.sleep(2000);
                System.out.println("This is Lazy method");
            } catch (InterruptedException interruptedException) {
 
            }
        }
    }
}

public class ThreadDemo {
	 
    public static void main(String[] args) throws InterruptedException {
 
        TestThread testThread = new TestThread();
        testThread.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
*/ 

Interrupting a thread

  1. we can interrupt a sleeping or waiting thread by using interrupt method of thread class.

    public void interrupt()

  2. When ever we are calling interrupt() method,the target thread may not be effected immediately.
  3. At the time of calling interrupt() method, if the target thread is not in sleeping or in waiting state interrupt call will wait until target thread entered into sleeping or waiting state.

Example:

class TestThread extends Thread {
    public void run() {
 
        try {
            for (int i = 1; i <= 5; i++) {
                System.out.println("This is Lazy method");
                Thread.sleep(5000);
            }
        } catch (InterruptedException interruptedException) {
            System.out.println(interruptedException);
        }
    }
}

public class ThreadDemo {
	 
    public static void main(String[] args) throws InterruptedException {
 
        TestThread testThread = new TestThread();
        testThread.start();
        testThread.interrupt();
        System.out.println("Main thread");
    }
}
/*
Output:
Main thread
This is Lazy method
java.lang.InterruptedException: sleep interrupted
*/

Comparison between yield(),join() and sleep()

Property yield() join() sleep()
is static? Yes No Yes
is overloaded? No Yes Yes
is final? No Yes No
is throws InterruptedException? No Yes Yes
is native? Yes No sleep(long ms)->native
sleep(long ms,int ms)->non native