interrupt()方法使用:
1. sleep() & interrupt()
线程A正在使用sleep()暂停着: Thread.sleep(100000);
如果要取消他的等待状态,可以在正在执行的线程里(比如这里是B)调用
a.interrupt();
令线程A放弃睡眠操作,这里a是线程A对应到的Thread实例
执行interrupt()时,并不需要获取Thread实例的锁定.任何线程在任何时刻,都可以调用其他线程interrupt().
当sleep中的线程被调用interrupt()时,就会放弃暂停的状态.并抛出InterruptedException.丢出异常的,是A线程.
2. wait() & interrupt()
线程A调用了wait()进入了等待状态,也可以用interrupt()取消.
不过这时候要小心锁定的问题.线程在进入等待区,会把锁定解除,
当对等待中的线程调用interrupt()时(注意是等待的线程调用其自己的interrupt()),
会先重新获取锁定,再抛出异常.在获取锁定之前,是无法抛出异常的.
3. join() & interrupt()
当线程以join()等待其他线程结束时,一样可以使用interrupt()取消之.
因为调用join()不需要获取锁定,故与sleep()时一样,会马上跳到catch块里.
注意是随调用interrupt()方法,一定是阻塞的线程来调用其自己的interrupt方法.
如在线程a中调用来线程t.join().则a会等t执行完后在执行t.join后的代码,
当在线程b中调用来a.interrupt()方法,则a会抛出InterruptedException
4. interrupt()只是改变中断状态而已
interrupt()不会中断一个正在运行的线程。
这一方法实际上完成的是,在线程受到阻塞时抛出一个中断信号,这样线程就得以退出阻塞的状态。
更确切的说,如果线程被Object.wait, Thread.join和Thread.sleep三种方法之一阻塞,
那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。
如果线程没有被阻塞,这时调用interrupt()后可以通过循环检查interrupted()方法,来中断操作。
否则,线程就将得到异常(该线程必须事先预备好处理此状况),接着逃离阻塞状态。
线程A在执行sleep,wait,join时,线程B调用A的interrupt方法,的确这一个时候A会有InterruptedException异常抛出来.
但这其实是在sleep,wait,join这些方法内部会不断检查中断状态的值,而自己抛出的InterruptedException。
如果线程A正在执行一些指定的操作时如赋值,for,while,if,调用方法,IO操作等,都不会去检查中断状态,所以线程A不会抛出InterruptedException,
而会一直执行着自己的操作.当线程A终于执行到wait(),sleep(),join()时,才马上会抛出InterruptedException.
若没有调用sleep(),wait(),join()这些方法,或是没有在线程里自己检查中断状态自己抛出InterruptedException的话,那InterruptedException是不会被抛出来的.
实例:
public class T extends Thread{
private Object o = new Object();
public void run() {
while(!interrupted()) //循环检查中断位是否被置位。如果置位则清除,并且退出循环。
{
try {
sleep(5000); //在sleep时,如果其它线程调用了T t的interrupt方法,则会直接跳到异常输出:sleep exception...。
} catch (InterruptedException e) {
System.out.println("sleep exception out...");
break;
}
synchronized (o) {//wait await 等操作必须在锁内。
try {
/**
* 或者await,join等阻塞操作,此时放弃锁的持有。
* 在等待对象 o 时,即等待其它线程调用o.signal()的时间段内,
* 如果其它线程调用了T t的interrupt方法
* 则t需要先获取到o的锁,然后才抛出异常。
*/
o.wait(5000);
} catch (InterruptedException e) {
System.out.println("wait exception out...");
break;
}
}
/**
* 这里还可以是IO等阻塞操作。
*/
for(int i = 0;i<999999999; i++){
int a = i * 876 / 123; //如果在此时其它线程调用了t.interrupt()方法,则会在下次while循环时,被检测出来,然后再退出循环。
}
System.out.println("success excute once!!");
}
System.out.println("exit");
}
public static void main(String... args){
T t = new T();
t.start();
InterruptT it = new InterruptT(t);
it.start();
}
}
class InterruptT extends Thread {
Thread t;
InterruptT(Thread t) {
this.t = t;
}
public void run(){
try {
// sleep(2000);//sleep exception out... exit
// sleep(8000);//wait exception out... exit
sleep(10800);//success excute once exit
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t.interrupt();
}
}
5、在一个递归调用方法时,若方法中有对锁对象的等待操作,则需要在捕获中断异常后,重新设置当前线程的中断标志位。即调用 Thread.currentThread().interrupt();
6、多线程环境下,条件等待状态下,当中断发生时,需要对等待条件在捕获异常处signal。以免在抛异常获取锁后,没有signal等待中的线程,他们会一直等待。
5和6的情况可以参看ArrayBlockingQueue类。
也可以参看下面的类。
public class LockTest {
private final ReentrantLock rLock = new ReentrantLock();
private final Condition aCondition = rLock.newCondition();
public void put(int i){
try{
rLock.lock();
System.out.println(i + " in put running....");
aCondition.await();//等待取。
System.out.println(i + " put await end......");
aCondition.signal();
}catch(InterruptedException e){
System.out.println(i + " put exception end....");
// e.printStackTrace();
// Thread.currentThread().interrupt();//循环调用,线程被中断后,按需求可以重置位中断位,好让所有循环都被中断。
aCondition.signal();//条件等待,多线程时,一个线程获取锁后抛异常,必须唤醒一个正在await的线程,否则会一直等下去。
}finally {
System.out.println(i + " put finally");
rLock.unlock();
}
}
public void get(int i){
try{
rLock.lock();
System.out.println(i + " in get running....");
aCondition.signal();//取了才能放。
System.out.println(i + " get signal end......");
}catch(Exception e){
e.printStackTrace();
}finally {
System.out.println(i + " get finally");
rLock.unlock();
}
}
public static void main(String[] args){
LockTest t = new LockTest();
// TPut tPut = new TPut(t);
// tPut.start();
// TGet tGet = new TGet(t);
// tGet.start();
TPut temp = null;
for(int i =0;i<5;i++){
TPut tPut = new TPut(t,i);
tPut.start();
temp = tPut;
}
TInterrupt tInterrupt = new TInterrupt(temp);
tInterrupt.start();
}
}
class TPut extends Thread {
LockTest t;
int i;
TPut(LockTest t,int i){
this.t = t;
this.i = i;
}
public void run(){
// for(int i =0;i<3;i++){
t.put(i);
// }
}
}
class TGet extends Thread {
LockTest t;
TGet(LockTest t){
this.t = t;
}
public void run(){
for(int i =0;i<3;i++){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t.get(i);
}
}
}
class TInterrupt extends Thread{
Thread t ;
TInterrupt(Thread t){
this.t = t;
}
public void run(){
t.interrupt();
}
}
1. sleep() & interrupt()
线程A正在使用sleep()暂停着: Thread.sleep(100000);
如果要取消他的等待状态,可以在正在执行的线程里(比如这里是B)调用
a.interrupt();
令线程A放弃睡眠操作,这里a是线程A对应到的Thread实例
执行interrupt()时,并不需要获取Thread实例的锁定.任何线程在任何时刻,都可以调用其他线程interrupt().
当sleep中的线程被调用interrupt()时,就会放弃暂停的状态.并抛出InterruptedException.丢出异常的,是A线程.
2. wait() & interrupt()
线程A调用了wait()进入了等待状态,也可以用interrupt()取消.
不过这时候要小心锁定的问题.线程在进入等待区,会把锁定解除,
当对等待中的线程调用interrupt()时(注意是等待的线程调用其自己的interrupt()),
会先重新获取锁定,再抛出异常.在获取锁定之前,是无法抛出异常的.
3. join() & interrupt()
当线程以join()等待其他线程结束时,一样可以使用interrupt()取消之.
因为调用join()不需要获取锁定,故与sleep()时一样,会马上跳到catch块里.
注意是随调用interrupt()方法,一定是阻塞的线程来调用其自己的interrupt方法.
如在线程a中调用来线程t.join().则a会等t执行完后在执行t.join后的代码,
当在线程b中调用来a.interrupt()方法,则a会抛出InterruptedException
4. interrupt()只是改变中断状态而已
interrupt()不会中断一个正在运行的线程。
这一方法实际上完成的是,在线程受到阻塞时抛出一个中断信号,这样线程就得以退出阻塞的状态。
更确切的说,如果线程被Object.wait, Thread.join和Thread.sleep三种方法之一阻塞,
那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。
如果线程没有被阻塞,这时调用interrupt()后可以通过循环检查interrupted()方法,来中断操作。
否则,线程就将得到异常(该线程必须事先预备好处理此状况),接着逃离阻塞状态。
线程A在执行sleep,wait,join时,线程B调用A的interrupt方法,的确这一个时候A会有InterruptedException异常抛出来.
但这其实是在sleep,wait,join这些方法内部会不断检查中断状态的值,而自己抛出的InterruptedException。
如果线程A正在执行一些指定的操作时如赋值,for,while,if,调用方法,IO操作等,都不会去检查中断状态,所以线程A不会抛出InterruptedException,
而会一直执行着自己的操作.当线程A终于执行到wait(),sleep(),join()时,才马上会抛出InterruptedException.
若没有调用sleep(),wait(),join()这些方法,或是没有在线程里自己检查中断状态自己抛出InterruptedException的话,那InterruptedException是不会被抛出来的.
实例:
public class T extends Thread{
private Object o = new Object();
public void run() {
while(!interrupted()) //循环检查中断位是否被置位。如果置位则清除,并且退出循环。
{
try {
sleep(5000); //在sleep时,如果其它线程调用了T t的interrupt方法,则会直接跳到异常输出:sleep exception...。
} catch (InterruptedException e) {
System.out.println("sleep exception out...");
break;
}
synchronized (o) {//wait await 等操作必须在锁内。
try {
/**
* 或者await,join等阻塞操作,此时放弃锁的持有。
* 在等待对象 o 时,即等待其它线程调用o.signal()的时间段内,
* 如果其它线程调用了T t的interrupt方法
* 则t需要先获取到o的锁,然后才抛出异常。
*/
o.wait(5000);
} catch (InterruptedException e) {
System.out.println("wait exception out...");
break;
}
}
/**
* 这里还可以是IO等阻塞操作。
*/
for(int i = 0;i<999999999; i++){
int a = i * 876 / 123; //如果在此时其它线程调用了t.interrupt()方法,则会在下次while循环时,被检测出来,然后再退出循环。
}
System.out.println("success excute once!!");
}
System.out.println("exit");
}
public static void main(String... args){
T t = new T();
t.start();
InterruptT it = new InterruptT(t);
it.start();
}
}
class InterruptT extends Thread {
Thread t;
InterruptT(Thread t) {
this.t = t;
}
public void run(){
try {
// sleep(2000);//sleep exception out... exit
// sleep(8000);//wait exception out... exit
sleep(10800);//success excute once exit
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t.interrupt();
}
}
5、在一个递归调用方法时,若方法中有对锁对象的等待操作,则需要在捕获中断异常后,重新设置当前线程的中断标志位。即调用 Thread.currentThread().interrupt();
6、多线程环境下,条件等待状态下,当中断发生时,需要对等待条件在捕获异常处signal。以免在抛异常获取锁后,没有signal等待中的线程,他们会一直等待。
5和6的情况可以参看ArrayBlockingQueue类。
也可以参看下面的类。
public class LockTest {
private final ReentrantLock rLock = new ReentrantLock();
private final Condition aCondition = rLock.newCondition();
public void put(int i){
try{
rLock.lock();
System.out.println(i + " in put running....");
aCondition.await();//等待取。
System.out.println(i + " put await end......");
aCondition.signal();
}catch(InterruptedException e){
System.out.println(i + " put exception end....");
// e.printStackTrace();
// Thread.currentThread().interrupt();//循环调用,线程被中断后,按需求可以重置位中断位,好让所有循环都被中断。
aCondition.signal();//条件等待,多线程时,一个线程获取锁后抛异常,必须唤醒一个正在await的线程,否则会一直等下去。
}finally {
System.out.println(i + " put finally");
rLock.unlock();
}
}
public void get(int i){
try{
rLock.lock();
System.out.println(i + " in get running....");
aCondition.signal();//取了才能放。
System.out.println(i + " get signal end......");
}catch(Exception e){
e.printStackTrace();
}finally {
System.out.println(i + " get finally");
rLock.unlock();
}
}
public static void main(String[] args){
LockTest t = new LockTest();
// TPut tPut = new TPut(t);
// tPut.start();
// TGet tGet = new TGet(t);
// tGet.start();
TPut temp = null;
for(int i =0;i<5;i++){
TPut tPut = new TPut(t,i);
tPut.start();
temp = tPut;
}
TInterrupt tInterrupt = new TInterrupt(temp);
tInterrupt.start();
}
}
class TPut extends Thread {
LockTest t;
int i;
TPut(LockTest t,int i){
this.t = t;
this.i = i;
}
public void run(){
// for(int i =0;i<3;i++){
t.put(i);
// }
}
}
class TGet extends Thread {
LockTest t;
TGet(LockTest t){
this.t = t;
}
public void run(){
for(int i =0;i<3;i++){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t.get(i);
}
}
}
class TInterrupt extends Thread{
Thread t ;
TInterrupt(Thread t){
this.t = t;
}
public void run(){
t.interrupt();
}
}