Skip to content

Commit

Permalink
改代码会产生死循环,但是得益于锁的响应中断,解决了死循环这个问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Pamgo committed Mar 6, 2018
1 parent b2ec220 commit 67ec80f
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/main/java/com/example/concurrency/st/interrupt/IntLock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.example.concurrency.st.interrupt;

import java.util.concurrent.locks.ReentrantLock;

/**
* 改代码会产生死循环,但是得益于锁的响应中断,解决了死循环这个问题
* @author OKali
*
*/
public class IntLock implements Runnable{

public static ReentrantLock lock1 = new ReentrantLock();
public static ReentrantLock lock2 = new ReentrantLock();

int lock;

/**
* 控制加锁顺序,方便构造死循环
* @param lock
*/
public IntLock(int lock) {
this.lock = lock;
}

@Override
public void run() {
try {
if (lock == 1) {
lock1.lockInterruptibly();
Thread.sleep(500);
lock2.lockInterruptibly();
} else {
lock2.lockInterruptibly();
Thread.sleep(500);
lock1.lockInterruptibly();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (lock1.isHeldByCurrentThread()) {
lock1.unlock();
}
if (lock2.isHeldByCurrentThread()) {
lock2.unlock();
}
System.out.println(Thread.currentThread().getId() + "线程退出!");
}
}

public static void main(String[] args) throws InterruptedException {
IntLock intLock = new IntLock(1);
Thread t1 = new Thread(intLock);
Thread t2 = new Thread(intLock);

t1.start();t2.start();
Thread.sleep(1000);
t1.interrupt();
}


}

0 comments on commit 67ec80f

Please sign in to comment.