62,614
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
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() + "退出");
}
}
}