Home > Java > javaTutorial > How to interrupt a running thread in Java (2)

How to interrupt a running thread in Java (2)

黄舟
Release: 2016-12-30 13:20:55
Original
1518 people have browsed it

Interrupting threads using Thread.interrupt()

As described in Listing A, the Thread.interrupt() method does not interrupt a running thread. What this method actually accomplishes is to throw an interrupt signal when the thread is blocked, so that the thread can exit the blocked state. To be more precise, if the thread is blocked by one of the three methods Object.wait, Thread.join and Thread.sleep, then it will receive an interrupt exception (InterruptedException), thereby terminating the blocked state early.

Therefore, if the thread is blocked by the above methods, the correct way to stop the thread is to set the shared variable and call interrupt() (note that the variable should be set first). If the thread is not blocked, calling interrupt() will have no effect; otherwise, the thread will get an exception (the thread must be prepared to handle this situation in advance) and then escape from the blocked state. In either case, eventually the thread will check the shared variable and then stop. Listing C is an example that describes this technique.

Listing C
class Example3 extends Thread {
  volatile boolean stop = false;
  public static void main( String args[] ) throws Exception {
   Example3 thread = new Example3();
   System.out.println( "Starting thread..." );
   thread.start();
   Thread.sleep( 3000 );
   System.out.println( "Asking thread to stop..." );
   thread.stop = true;//如果线程阻塞,将不会检查此变量
   thread.interrupt();
   Thread.sleep( 3000 );
   System.out.println( "Stopping application..." );
   //System.exit( 0 );
  }
  public void run() {
    while ( !stop ) {
     System.out.println( "Thread running..." );
      try {
      Thread.sleep( 1000 );
      } catch ( InterruptedException e ) {
      System.out.println( "Thread interrupted..." );
      }
    }
   System.out.println( "Thread exiting under request..." );
  }
}
Copy after login

Once Thread.interrupt() in Listing C is called, the thread receives an exception, escapes the blocking state and determines that it should stop. Running the above code will get the following output:

Starting thread...
Thread running...
Thread running...
Thread running...
Asking thread to stop...
Thread interrupted...
Thread exiting under request...
Stopping application...
Copy after login

The above is the content of how to interrupt a running thread (2) in Java. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template