MOCKSTACKS
EN
Questions And Answers

More Tutorials









Java Thread Methods


Join() method In Java :


The join() method in Java allows one thread to wait until the execution of some other specified thread is completed.
If t is a Thread object whose thread is currently executing, then t.join() causes the current thread to pause execution until t's thread terminates.
Syntax :
Join() method puts the current thread on wait until the thread on which it is called is dead.
public final void join()

You can also specify the time for which you need to wait for the execution of a particular thread by using the Join() method.
Syntax :
public final void join(long millis)

Sleep() Method :


The sleep() method in Java is useful to put a thread to sleep for a specified amount of time.
When we put a thread to sleep, the thread scheduler picks and executes another thread in the queue.
Sleep() method returns void.
sleep() method can be used for any thread, including the main() thread also.

Syntax :
public static void sleep(long milliseconds)throws InterruptedException
public static void sleep(long milliseconds, int nanos)throws InterruptedException

Parameters Passed To Sleep() Method :
long millisecond: Time in milliseconds for which thread will sleep.
nanos : Ranges from [0,999999]. Additional time in nanoseconds.

Example :
import java.io.*;
import java.lang.Thread;
public class cwh {
    public static void main(String[] args)
    {
        try {
            for (int i = 1; i <=5; i++) {
                Thread.sleep(2000);
                System.out.println(i);
            }
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

In the above example, the main() method will be put to sleep for 2 seconds every time the for loop executes.

Output

1
2
3
4
5

Interrupt() method :


A thread in a sleeping or waiting state can be interrupted by another thread with the help of the interrupt() method of the Thread class.
The interrupt() method throws InterruptedException.
The interrupt() method will not throw the InterruptedException if the thread is not in the sleeping/blocked state, but the interrupt flag will be changed to true.
Syntax :
Public void interrupt()

Different scenarios where Interrupt() method can be used:

Case 1: Interrupting a thread that doesn’t stop working :


class CWH1 extends Thread{
    public void run(){
        try {
            for (int i=0;i<5;i++){
                System.out.println("Child Thread");
                Thread.sleep(4000); /* Child thread is put to sleep for 4000ms. As soon as child thread goes to sleep main thread interrupts it. And, InterruptedException is generated which is handled by the catch block. */

            }
        }
        catch (InterruptedException e)
        {
            System.out.println("Child Thread Interrupted");
        }
	System.out.println("Thread is running");

    }
}

public class CWH extends Thread{
    public static void main(String[] args) {
         CWH1 t= new CWH1();
         t.start();
         t.interrupt();
        System.out.println("Main Thread");

    }
}

In the above code, the for loop runs for the first time, but the child thread is put to sleep after that. So, the main() method interrupts the child thread, and InterruptedException is generated. Here, the child thread comes out of the sleeping state, but it does not stop working.

Output

Main Thread
Child Thread
Child Thread Interrupted
Thread is running

Case 2: Interrupting a thread that works normally :


class CWH1 extends Thread{
    public void run(){
        for (int i=0;i<10;++i){
            System.out.println(i);
        }
    }
}

public class CWH extends Thread{
    public static void main(String[] args) {
         CWH1 t= new CWH1();
         t.start();
         t.interrupt();
        System.out.println("Main Thread");

    }
}

Here the thread works normally because no exception occurred during the thread's execution, so the interrupt() only sets the value of the thread flag to true.

Output

0
1
2
3
4
5
6
7
8
9

Example


package com.company;

class MyNewThr1 extends Thread{
    public void run(){
        int i = 0;
        while(true){
//            System.out.println("I am a thread");
            System.out.println("Thank you: ");
            try {
                Thread.sleep(455);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            i++;
        }
    }
}

class MyNewThr2 extends Thread{

    public void run(){
        while(true){
//            System.out.println("I am a thread");
            System.out.println("My Thank you: ");
        }
    }
}

public class thread_methods {
    public static void main(String[] args){
        MyNewThr1 t1 = new MyNewThr1();
        MyNewThr2 t2 = new MyNewThr2();
        t1.start();
//        try{
//            t1.join();
//        }
//        catch(Exception e){
//            System.out.println(e);
//        }

        t2.start();

    }
}


Conclusion

In this page (written and validated by ) you learned about Java Thread Methods . What's Next? If you are interested in completing Java tutorial, your next topic will be learning about: Java Errors and Exception.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.