How to break blocking with java interrupt ()
This article introduces the knowledge of "how to break blocking with java interrupt ()". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Description
1. Call interrupt () to break the blocking, which does not mean the end of the thread's life cycle, but only interrupts the blocking state of the current thread.
2. Once interrupted in the blocking state, an InterruptedException exception is thrown, which signals that the current thread has been interrupted.
Example
Public static void main (String [] args) throws InterruptedException {Thread thread = new Thread (()-> {try {TimeUnit.SECONDS.sleep (10);} catch (InterruptedException e) {System.out.println ("Thread is interrupted.");}}); thread.start (); TimeUnit.SECONDS.sleep (1); thread.interrupt () } this is the end of "how to break blocking with java interrupt ()". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!