Home >Java >javaTutorial >What is the difference between Java thread interruption and blocking? Comparison of Java thread interruption and blocking
What this article brings to you is what is the difference between Java thread interruption and blocking? The comparison between Java thread interruption and blocking has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Many Java developers (including me), especially novices who have just entered the software industry, think that setting thread interruption in Java means that the thread has stopped and will not execute forward.
Thread.currentThread().interrupt()
Actually, this is not the case. Thread interruption is just a state. true means it has been interrupted, false means it has not been interrupted.
//Get the thread interruption status, if Returns true if interrupted, otherwise returns false
Thread.currentThread().isInterrupted()
Setting the thread interrupt does not affect the continued execution of the thread, but after the thread sets the interrupt, the wait, jion, and sleep methods are called in the thread. A method that immediately throws an InterruptedException, and the interrupt flag is cleared and reset to false.
class Thread2 implements Runnable{
@Override public void run() { try { System.out.println(); System.out.println(hread.currentThread().isInterrupted());//输出false Thread.currentThread().interrupt();//当前线程中断 System.out.println("Thread.currentThread().isInterrupted());//输出true Thread.sleep(3000);//中断后执行sleep会抛出异常 } catch (InterruptedException e) { e.printStackTrace(); System.out.println("Thread.currentThread().isInterrupted());//输出false } } }
How to make the thread really stop and not execute forward:
To really make the thread stop (block), Java provides a relatively low-level concurrency Tool class: LockSupport, there are two commonly used methods in this class, 1
park(Object blocker) means blocking the specified thread, the parameter blocker is the current thread object 2 unpark(Thread thread) wakes up the specified thread, the parameter thread specifies the thread Object
Example:
public void test_LockSupport(){ Thread thread=new Thread(new Thread_park()); thread.start();//阻塞当前线程 Thread thread2=new Thread(new Thread_unpark(thread)); thread2.start();//唤醒被阻塞的线程 } class Thread_park implements Runnable{ @Override public void run() { System.out.println("Thread_park开始"); LockSupport.park(this);//阻塞当前线程 System.out.println("Thread_park结束"); } } class Thread_unpark implements Runnable{ private Thread thread; public Thread_unpark(Thread thread) { this.thread = thread; } @Override public void run() { System.out.println("Thread_unpark开始"); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } LockSupport.unpark(thread);//唤醒被阻塞的线程 System.out.println("Thread_unpark结束"); } }
Related recommendations:
Detailed explanation of thread blocking usage of LockSupport class in Java multi-thread programming
Sample code for multi-thread blocking and wake-up in Java
The above is the detailed content of What is the difference between Java thread interruption and blocking? Comparison of Java thread interruption and blocking. For more information, please follow other related articles on the PHP Chinese website!