资源简介
实战JAVA高并发程序设计源代码汇总版
代码片段和文件信息
package geym.ch10;
import java.util.concurrent.locks.ReentrantLock;
public class DeadLockCar extends Thread{
protected object myDirect;
static ReentrantLock south = new ReentrantLock();
static ReentrantLock north = new ReentrantLock();
static ReentrantLock west = new ReentrantLock();
static ReentrantLock east = new ReentrantLock();
public DeadLockCar(object obj){
this.myDirect=obj;
if(myDirect==south){
this.setName(“south“);
}
if(myDirect==north){
this.setName(“north“);
}
if(myDirect==west){
this.setName(“west“);
}
if(myDirect==east){
this.setName(“east“);
}
}
@Override
public void run() {
if (myDirect == south) {
try {
west.lockInterruptibly();
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
south.lockInterruptibly();
System.out.println(“car to south has passed“);
} catch (InterruptedException e1) {
System.out.println(“car to south is killed“);
}finally{
if(west.isHeldByCurrentThread())
west.unlock();
if(south.isHeldByCurrentThread())
south.unlock();
}
}
if (myDirect == north) {
try {
east.lockInterruptibly();
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
north.lockInterruptibly();
System.out.println(“car to north has passed“);
} catch (InterruptedException e1) {
System.out.println(“car to north is killed“);
}finally{
if(north.isHeldByCurrentThread())
north.unlock();
if(east.isHeldByCurrentThread())
east.unlock();
}
}
if (myDirect == west) {
try {
north.lockInterruptibly();
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
west.lockInterruptibly();
System.out.println(“car to west has passed“);
} catch (InterruptedException e1) {
System.out.println(“car to west is killed“);
}finally{
if(north.isHeldByCurrentThread())
north.unlock();
if(west.isHeldByCurrentThread())
west.unlock();
}
}
if (myDirect == east) {
try {
south.lockInterruptibly();
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
east.lockInterruptibly();
System.out.println(“car to east has passed“);
} catch (InterruptedException e1) {
System.out.println(“car to east is killed“);
}finally{
if(south.isHeldByCurrentThread())
south.unlock();
if(east.isHeldByCurrentThread())
east.unlock();
}
}
}
public static void main(String[] args) throws InterruptedException {
DeadLockCar car2south = new DeadLockCar(south);
DeadLockCar car2north = new DeadLockCar(north);
DeadLockCar car2west = new DeadLockCar(west);
DeadLockCar car2east = new DeadLockCar(east);
car2south.start();
car2north.start();
car2west.start();
car2east.start();
Thread.sleep(1
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1372 2018-08-06 12:29 ch2\AccountingVol.java
文件 1300 2018-08-06 12:29 ch2\AccountSync.java
文件 1669 2018-08-06 12:29 ch2\ArrayListMultiThread.java
文件 1359 2018-08-06 12:29 ch2\BadLockOnInteger.java
文件 695 2015-05-29 20:18 ch2\BadSuspend.java
文件 730 2018-08-06 12:29 ch2\CreateThread.java
文件 993 2018-08-06 12:29 ch2\DaemonDemo.java
文件 1596 2018-08-06 12:29 ch2\HashMapMultiThread.java
文件 969 2018-08-06 12:29 ch2\InterruptAndStopThread.java
文件 734 2018-08-06 12:29 ch2\InterruptThread.java
文件 806 2018-08-06 12:29 ch2\JoinMain.java
文件 1438 2018-08-06 12:29 ch2\PriorityDemo.java
文件 1509 2018-08-06 12:29 ch2\SimpleWaitAndNotify.java
文件 1320 2015-05-29 21:51 ch2\SimpleWN.java
文件 1230 2015-05-29 20:55 ch2\SimpleWNA.java
文件 2051 2018-08-06 12:29 ch2\StopThreadUnsafe.java
文件 492 2015-05-29 21:43 ch2\sync\AccountingSync.java
文件 520 2015-05-29 20:58 ch2\sync\AccountingSync2.java
文件 495 2015-05-29 21:46 ch2\sync\AccountingSyncBad.java
文件 508 2015-05-29 20:58 ch2\sync\AccountingSyncClass.java
文件 1390 2018-08-06 12:29 ch2\ThreadGroupName.java
文件 1608 2018-08-06 12:29 ch2\VolatileDemo.java
文件 703 2015-05-31 18:07 ch4\AtomicIntegerArrayDemo.java
文件 795 2018-08-06 12:29 ch4\AtomicIntegerDemo.java
文件 1251 2018-08-06 12:29 ch4\AtomicIntegerFieldUpdaterDemo.java
文件 882 2018-08-06 12:29 ch4\AtomicInterArrayDemo.java
文件 1060 2015-05-31 17:36 ch4\AtomicReferenceTest.java
文件 1987 2018-08-06 12:29 ch4\AtomicRefrenceDemo.java
文件 2149 2018-08-06 12:29 ch4\AtomicStampedReferenceDemo.java
文件 1474 2018-08-06 12:29 ch4\DeadLock.java
............此处省略95个文件信息
评论
共有 条评论