Java Thread Methods - sleep stop yield

Advertisements

yield(), stop() and sleep() methods of Thread class.

We have discussed before, the start() and run() methods of Thread class. Now we will discuss few more methods of Thread class such as:

  • sleep() method
  • stop() method
  • yield() method

yield() : Thread Scheduler is responsible for releasing and occupying the thread, we can't tell which thread will execute first and which thread will execute second. Even we can't guess when JVM will release the current thread and occupy the other thread. The yield() method can be used to explicitly release the control from current thread.

stop() : The stop() can be used to abort the thread.

sleep() : The sleep() can be used to block the thread for specific number of milliseconds. We must put sleep() method in try-catch block, b'coz the sleep() method throws an exception, which should be caught otherwise program will not compile.

Example of thread methods


//ThreadMethodsDemo.java

    class Pattern
    {
        public void DisplayA()
        {
            for(int i=1;i<=10;i++)
            {
                System.out.println("A : " + i);

                try{Thread.sleep(100);}catch(Exception Ex){}
            }
        }

        public void DisplayB()
        {
            for(int i=1;i<=10;i++)
            {
                System.out.println("B : " + i);

                if(i==5)
                Thread.currentThread().yield();
            }
        }

        public void DisplayC()
        {
            for(int i=1;i<=10;i++)
            {
                System.out.println("C : " + i);

                if(i==5)
                Thread.currentThread().stop();
            }
        }
    }

    class ThreadA extends Thread
    {
        Pattern P;

        ThreadA(Pattern P)
        {
            this.P = P;
        }

        public void run()
        {
            P.DisplayA();
        }
    }

    class ThreadB extends Thread
    {
        Pattern P;

        ThreadB(Pattern P)
        {
            this.P = P;
        }

        public void run()
        {
            P.DisplayB();
        }
    }

    class ThreadC extends Thread
    {
        Pattern P;

        ThreadC(Pattern P)
        {
            this.P = P;
        }

        public void run()
        {
            P.DisplayC();
        }
    }

    class ThreadDemo
    {
        public static void main(String arg[])
        {

            Pattern P = new Pattern();

            ThreadA t1 = new ThreadA(P);    //Statement 1
            ThreadB t2 = new ThreadB(P);    //Statement 2
            ThreadC t3 = new ThreadC(P);    //Statement 3

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

        }
    }

	
Output 1: Output 2: Output 3: Output 4:
A - 1 C - 1 C - 2 B - 1 B - 2 B - 3 B - 4 B - 5 C - 3 C - 4 C - 5 B - 6 B - 7 B - 8 B - 9 B - 10 A - 2 A - 3 A - 4 A - 5 A - 6 A - 7 A - 8 A - 9 A - 10 B - 1 B - 2 B - 3 B - 4 A - 1 B - 5 C - 1 B - 6 C - 2 B - 7 C - 3 B - 8 C - 4 B - 9 C - 5 B - 10 A - 2 A - 3 A - 4 A - 5 A - 6 A - 7 A - 8 A - 9 A - 10 B - 1 B - 2 B - 3 B - 4 B - 5 C - 1 C - 2 A - 1 C - 3 B - 6 C - 4 B - 7 C - 5 B - 8 B - 9 B - 10 A - 2 A - 3 A - 4 A - 5 A - 6 A - 7 A - 8 A - 9 A - 10 A - 1 C - 1 C - 2 C - 3 C - 4 C - 5 B - 1 B - 2 B - 3 B - 4 B - 5 B - 6 B - 7 B - 8 B - 9 B - 10 A - 2 A - 3 A - 4 A - 5 A - 6 A - 7 A - 8 A - 9 A - 10

In the above outputs, every time ThreadB has relinquished its control to other thread, when the value of i has reached at 5. Consider the output4, both B - 5 and B - 6 comes together b'coz ThreadC has been already stopped due to stop() method and ThreadA is still sleeping due to sleep() method.

Advertisement