Detailed explanation of several commonly used APIs in Thread: join and interrupt

1.join()

The join method can help us achieve the synchronization effect of calling threads in multi-threads. For example, there are three threads, a, b, c.

In a, threads b and c are started to execute one thing asynchronously, but a hopes that they will continue to execute after they finish. Then, after calling b and c to start, call the join method of each thread, which means waiting. After they are executed, the code is as follows:

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread() {
            @Override
            public void run() {
                System.out.println("我是线程1");
            }
        };
        Thread thread1 = new Thread() {
            @Override
            public void run() {
                System.out.println("我是线程2");
            }
        };
        thread.join();
        thread1.join();

        System.out.println("我是主线程");
    }

2.interrupt()

This method can be understood as interrupting the current state of the thread. There are several scenarios: We can usually use one thread to control whether another thread is running, and call interrupt() when another thread needs to be notified, and then There is a judgment logic in the stopped thread to judge the status of the current thread.

  • Interrupt the thread in the blocked state, such as calling the sleep or yield method, etc. If at this time, another thread calls the interrupt() method of the thread, an interruptedException will be thrown, and the interrupt status will be changed to false
  • Interrupting a running thread means that other code is being executed and is not in sleep or other states. At this time, the value of thread1.isInterrupted() will be changed to true.
  • Interrupt park. Park blocks the current thread. Calling the thread's interrupt() will allow it to continue running downward. But please note that whether the park method continues to run downward depends on whether the current thread thread1.isInterrupted() Is true. If it is true, it cannot block again. This means that if a thread calls the LockSupport.park() method once and another thread interrupts it, then when it continues to run, it calls it again. The LockSupport.park() method cannot be blocked because the isInterrupted() of the current thread is true at this time, so if you want to run it again, you can call Thread.Interrupted() after the first park() method. ) method can help us change the interruption status to false, so that the program can block again when calling the park() method. The code is roughly as follows
        public static void main(String[] args) throws InterruptedException {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    LockSupport.park();
    
                    System.out.printf("当前打断状态为%s\n",Thread.interrupted());
                    
                    LockSupport.park();
                    System.out.println("dssdsds");
                }
            };
            thread.start();
            thread.interrupt();
    
        }

Guess you like

Origin blog.csdn.net/weixin_59244784/article/details/132762250