test;
public class ThreadTest extends Thread{
//线程中断信号量
volatile boolean stop=false;
public static void main(String[] args) throws Exception {
ThreadTest thread=new ThreadTest();
System.out.println("Starting thread...");
thread.start();
Thread.sleep(3000);
System.out.println("Asking thread to stop...");
// 设置中断信号量
thread.stop = true;
Thread.sleep(3000);
System.out.println("Stopping application...");
}
@Override
public void run() {
//每隔一秒检测一下中断信号量
while(!stop){
System.out.println("Thread is running!");
long begin=System.currentTimeMillis();
/**
* 使用while循环模拟sleep方法,这里不要使用sleep,否则在阻塞时会抛InterruptedException异常而退出循环,
* 这样while检测stop条件就不会执行,失去了意义。
*/
while ((System.currentTimeMillis() - begin < 1000)) {
}
}
System.out.println("Thread exiting under request!");
}
}
运行结果如下:
使用thread.interrupt()中断非阻塞状态线程
虽然上面案例要求一些编码,但并不难实现。同时,它给予线程机会进行必要的清理工作。这里需注意一点的是需将共享变量定义成volatile 类型或将对它的一切访问封入同步的块/方法(synchronized blocks/methods)中。上面是中断一个非阻塞状态的线程的常见做法,但对非检测isInterrupted()条件会更简洁:
package com.ljq.test;
public class ThreadTest extends Thread{
public static void main(String[] args) throws Exception {
ThreadTest thread=new ThreadTest();
System.out.println("Starting thread...");
thread.start();
Thread.sleep(3000);
System.out.println("Asking thread to stop...");
// 发出中断请求
thread.interrupt();
Thread.sleep(3000);
System.out.println("Stopping application...");
}
@Override
public void run() {
//每隔一秒检测一下中断信号量
while(!Thread.currentThread().isInterrupted()){
System.out.println("Thread is running!");
long begin=System.currentTimeMillis();
/**
* 使用while循环模拟sleep方法,这里不要使用sleep,否则在阻塞时会抛InterruptedException异常而退出循环,
* 这样while检测stop条件就不会执行,失去了意义。
*/
while ((System.currentTimeMillis() - begin < 1000)) {
}
}
System.out.println("Thread exiting under request!");
}
}
到目前为止一切顺利!但是,当线程等待某些事件发生而被阻塞,又会发生什么?当然,如果线程被阻塞,它便不能核查共享变量,也就不能停止。这在许多情况下会发生,例如调用Object.wait()、ServerSocket.accept()和DatagramSocket.receive()时,这里仅举出一些。
他们都可能永久的阻塞线程。即使发生超时,在超时期满之前持续等待也是不可行和不适当的,所以,要使用某种机制使得线程更早地退出被阻塞的状态。下面就来看一下中断阻塞线程技术。
使用thread.interrupt()中断阻塞状态线程
Thread.interrupt()方法不会中断一个正在运行的线程。这一方法实际上完成的是,设置线程的中断标示位,在线程受到阻塞的地方(如调用sleep、wait、join等地方)抛出一个异常InterruptedException,并且中断状态也将被清除,这样线程就得以退出阻塞的状态。下面是具体实现:
package com.ljq.test;
public class ThreadTest extends Thread{
public static void main(String[] args) throws Exception {
ThreadTest thread=new ThreadTest();
System.out.println("Starting thread...");
thread.start();
Thread.sleep(3000);
thread.interrupt();// 等中断信号量设置后再调用
System.out.println("Asking thread to stop...");
Thread.sleep(3000);
System.out.pri