Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Will threads continue after thread.join (long) times out?

The interpretation of the join ()

Join () is a method of the Thread class that is used by one Thread to listen on methods of another Thread.

For example, if thread A reaches threadb.join (), thread A will wait for threadB to complete before executing the statement after threadb.join (). The way to view it is described like this:

Waits for this thread to die. An invocation of this method behaves in exactly the same way as the invocation join(0) Throws: InterruptedException -- If any thread has interrupted the current thread. The interrupted status of the current thread is  cleared when this exception is thrown.Copy the code

The translation is

Wait for the thread to die. This method is called exactly as the call behaves. Join (0) throws: InterruptedException -- if any thread interrupts the current thread. When this exception is thrown, the interrupted state of the current thread is cleared.Copy the code

So the focus should be on the method explanation of join(long)

Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever. This implementation uses  a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances. Params: Millis -- The time to wait in milliseconds Throws: IllegalArgumentException -- if the value of millis is negative InterruptedException -- if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown. The time to wait for this thread to die is at most milliseconds. Timeout 0: wait forever. This implementation uses a loop called this. wait that is conditional on this. isAlive. When the thread terminates, the this.notifyAll method is called. Applications are advised not to use Wait, notify, or notifyAll on thread instances. Arguments: ms - Wait time in milliseconds Throw: IllegalArgumentException - if the value of ms is negative InterruptedException - if any thread interrupts the current thread. When this exception is thrown, the interrupted state of the current thread is cleared.Copy the code

The join (long)

Thread provides join() as well as join(long millis) and JOIN (Long millis,int nanos).

If a thread does not terminate at the specified time, it will time out, and then execute the following program.

Does thread A stop after the timeout? Don’t

This is where it’s easy to forget that the concept of timeout is not thread cancellation

class Test1 { public static void main(String[] args) { Test1.AThread aThread = new Test1.AThread(); aThread.start(); try { aThread.join(1000); System.out.println(" Main thread completes execution "); } catch (InterruptedException e) { e.printStackTrace(); System.out.println("main thread execution exception "); } } static class AThread extends Thread { public AThread() { super("AThread"); } public void run() { try { Thread.sleep(2000); System.out.println("A thread completes execution "); } catch (InterruptedException e) {system.out.println ("A thread is executing an exception "); }}}}Copy the code

Output:

The execution of main thread ends. The execution of THREAD A ends