Java high concurrency 3-interrupt thread and the difference between isinterrupted and interrupted

Time:2021-1-26

1、 Review the previous section

  • The wait () method and interrupt are thrown by the running thread java.lang.InterruptedException When the thread calls the wait() method of the shared object, the current thread will only release the lock of the current shared variable, not the lock of other shared variables held by the thread.
  • Wait (long timeout, int nanos) implementation, wait (0) internal call of wait() method, notify() random wake-up, notifyall() all wake-up, join method, sleep method, yield method, and the difference between sleep and yield method

2、 Thread interrupt

  • Use interrupt () method to interrupt the thread, isinterrupted () to detect whether the thread has been interrupted
package com.ruigege.threadFoundation1;

public class SubThreadInterruptedState {
	public static void main(String[] args) throws InterruptedException{
		Thread thread1 = new Thread(new Runnable() {
			@Override
			public void run() {
				while(!Thread.currentThread().isInterrupted()) {
					System.out.println (the "child thread has no interrupt");
				}
				System.out.println (although the child thread is interrupted, it does not end);
			}
		});
		
		thread1.start();
		Thread.sleep (5) ; // ensure that the child thread can be executed
		Thread1. Interrupt(); // interrupt the sub thread. It can be understood that the time slice is not given here, but will be given later. Therefore, the sub thread will continue to run and end. This function is just a cousin
		System.out.println(thread1.isInterrupted());
		thread1.join();
		System.out.println (end of main thread);
		
	}
}

3.1

  • Explanation:
    • The execution of void interrupt() method can be understood as making a mark instead of stopping the thread. The thread will continue to execute until the end
    • The Boolean isinterrupted() method is used to detect the tag. If it is interrupted, it returns true; otherwise, it returns false
    • The static Boolean interrupted() method is also used to detect the mark. However, there are many steps to remove the mark. I will give an example later.
public static boolean Interrupted(){
	return Thread.currentThread().isInterrupted(true);
}

1. When calling interrupt () method, where will the thread interrupt?

  • When a thread calls the wait (), sleep (), and join () methods, if other threads give it the interrupt method, it will interrupt at the first three methods
  • Let’s take the above example directly. For example, if a thread is executed and asked to sleep in the middle for a few seconds, then interrupt is used to interrupt and a message is thrown in the sleep function java.lang.InterruptedException
package com.ruigege.threadFoundation1;

public class InterruptThreadWhenSleeping {
	public static void main(String[] args) throws InterruptedException {
		Thread threadOne = new Thread(new Runnable() {
			@Override
			public void run(){
				try {
					System.out.println ("the child thread starts and starts sleeping for a few seconds");
					Thread.sleep(3000);
					System.out.println (end of sleep);
				}catch(InterruptedException e) {
					e.printStackTrace();
				}

			}
		});
		
		threadOne.start();
		Thread.sleep(1000);
		threadOne.interrupt();
		threadOne.join();
		
	}
}

3.2

  • When I wrote this example, I wanted to use run() throws interruptedexception, but the compilation failed, so I used try… Catch… To catch the exception, and Baidu gave it a tryAVA run() method cannot throw exceptionIn fact, I didn’t understand it. It should be the principle of JVM. I have to continue to learn it.
  • The difference between Boolean isinterrupted() and Boolean interrupted() is that the former is to detect the interrupt flag, and the latter is also to detect the interrupt flag. However, after detection, the flag will be removed. Let’s take a direct example
package com.ruigege.threadFoundation1;

public class isInterruptedAndinterrupted {
	public static void main(String[] args) throws InterruptedException {
		Thread threadOne = new Thread(new Runnable() {
			@Override
			public void run() {
				for(;;) {
					
				}
			}
		});
		
		threadOne.start();
		threadOne.interrupt();
		
		System.out.println (child thread isinterrupted:+ threadOne.isInterrupted ());
		System.out.println (child interrupted:+ threadOne.interrupted ());
		System.out.println (main thread interrupted:+ Thread.interrupted ());
		System.out.println (child thread isinterrupted:+ threadOne.isInterrupted ());
		threadOne.join();
	}
}

3.3

  • Pay attention to the second one, why is it false? We need to look at the source code, return Thread.currentThread (). Isinterrupted (), so although the child thread object is called, it runs the statement in the main thread, so it is false
  • Let’s take a look at one
package com.ruigege.threadFoundation1;

public class SubThreadInvokeTag {
	public static void main(String[] args) throws InterruptedException {
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				while(!Thread.currentThread().isInterrupted()) {
					
				}
				System.out.println (child isinterrupted() state:+ Thread.currentThread ().isInterrupted());
				System.out.println (child thread interrupted() state:+ Thread.currentThread ().interrupted());
				System.out.println (child isinterrupted() state:+ Thread.currentThread ().isInterrupted());
			}
		});
		thread.start();
		thread.interrupt();
		System.out.println (main thread isinterrupted state)+ Thread.currentThread ().isInterrupted());
		thread.join();
		
	}
}

3.4

3、 Source code: