线程不能被interrupt中断

datadev_sh 2018-02-26 10:17:31
如下代码,main方法中的休眠时间长短,会影响到是否能被中断,这是为什么?

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo implements Runnable {

private static ReentrantLock lock1 = new ReentrantLock();
private static ReentrantLock lock2 = new ReentrantLock();
int lock;

public ReentrantLockDemo(int lock) {
this.lock = lock;
}

@Override
public void run() {
try {
if (lock == 1) {
lock1.lockInterruptibly();
try {
Thread.sleep(500);
} catch (Exception e) {

}
lock2.lockInterruptibly();
} else {
lock2.lockInterruptibly();
try {
Thread.sleep(500);
} catch (Exception e) {

}
lock1.lockInterruptibly();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (lock1.isHeldByCurrentThread())
lock1.unlock();
if (lock2.isHeldByCurrentThread())
lock2.unlock();
System.out.println(Thread.currentThread().getName() + "退出");
}
}

public static void main(String[] args) throws Exception {
ReentrantLockDemo r1 = new ReentrantLockDemo(1);
ReentrantLockDemo r2 = new ReentrantLockDemo(2);

Thread t1 = new Thread(r1, "t1");
Thread t2 = new Thread(r2, "t2");
t1.start();
t2.start();
Thread.sleep(100);
t2.interrupt();
}
}

当休眠100毫秒是,发送死锁。当不休眠,或者休眠1000毫秒时,程序可以正常结束。


...全文
306 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
datadev_sh 2018-02-26
  • 打赏
  • 举报
回复
sleep过程中被中断,没有finally块来释放锁。
soton_dolphin 2018-02-26
  • 打赏
  • 举报
回复
多加入一些注释,再用不同的时间让主线程休眠,你就能发现问题了。

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo implements Runnable {
	 
    private static ReentrantLock lock1 = new ReentrantLock();
    private static ReentrantLock lock2 = new ReentrantLock();
    int lock;
 
    public ReentrantLockDemo(int lock) {
        this.lock = lock;
    }
 
    
    public static void main(String[] args) throws Exception {
        ReentrantLockDemo r1 = new ReentrantLockDemo(1);
        ReentrantLockDemo r2 = new ReentrantLockDemo(2);
 
        Thread t1 = new Thread(r1, "t1");
        Thread t2 = new Thread(r2, "t2");
        t1.start();
        t2.start();
        System.out.println("线程 " + Thread.currentThread().getName() + " 将会休眠1秒");
        Thread.sleep(1000);
        System.out.println("线程 " + Thread.currentThread().getName() + " 已经苏醒。");
        t1.interrupt();
    }

    @Override
	public void run() {
    	String currentThreadName = Thread.currentThread().getName();
    	System.out.println("线程 " + currentThreadName + " 正在执行.");
    	 try {
             if (lock == 1) {
                 lock1.lockInterruptibly();
                 System.out.println("lock1 在线程  " + currentThreadName + "获得锁。");
                 try {
                	 System.out.println("线程 " + currentThreadName + " 将会休眠0.5秒");
                     Thread.sleep(500);
                     System.out.println("线程 " + currentThreadName + " 已经苏醒.");
                 } catch (Exception e) {
                	 System.err.println("线程 " + currentThreadName + " 被打断.");
                 }
                 lock2.lockInterruptibly();
                 System.out.println("lock2 在线程 " + currentThreadName + "获得锁。");
             } else {
                 lock2.lockInterruptibly();
                 System.out.println("lock2 在线程 " + currentThreadName + "获得锁。");
                 try {
                	 System.out.println("线程 " + currentThreadName + " 将会休眠0.5秒");
                     Thread.sleep(500);
                     System.out.println("线程 " + currentThreadName + " 已经苏醒.");
                 } catch (Exception e) {
                	 System.err.println("线程 " + currentThreadName + " 被打断.");
                 }
                 lock1.lockInterruptibly();
                 System.out.println("lock1 is locked in " + currentThreadName);
             }
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             if (lock1.isHeldByCurrentThread()){
                 lock1.unlock();
                 System.out.println("lock1 在线程  " + currentThreadName + "解锁");
             }
             if (lock2.isHeldByCurrentThread()){
                 lock2.unlock();
                 System.out.println("lock2 在线程  " + currentThreadName + "解锁");
             }
             System.out.println(Thread.currentThread().getName() + "退出");
         }
		
	}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧