/*
Java Threads, 3rd Edition
By Scott Oaks, Henry Wong
3rd Edition September 2004
ISBN: 0-596-00782-5 */
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.*; //
// This is a very very slow implementation of a ReentrantLock class and is not
// for
// everyday usage. The purpose of this class is to test for deadlocks. The
// lock()
// method now throws a DeadlockDetectedException, if a deadlock occurs.
//
public class DeadlockDetectingLock extends ReentrantLock {
// List of deadlock detecting locks.
// This array is not thread safe, and must be externally synchronized
// by the class lock. Hence, it should only be called by static
// methods.
private static List deadlockLocksRegistry = new ArrayList(); private static synchronized void registerLock(DeadlockDetectingLock ddl) {
if (!deadlockLocksRegistry.contains(ddl))
deadlockLocksRegistry.add(ddl);
} private static synchronized void unregisterLock(DeadlockDetectingLock ddl) {
if (deadlockLocksRegistry.contains(ddl))
deadlockLocksRegistry.remove(ddl);
} // List of threads hard waiting for this lock.
// This array is not thread safe, and must be externally synchronized
// by the class lock. Hence, it should only be called by static
// methods.
private List hardwaitingThreads = new ArrayList(); private static synchronized void markAsHardwait(List l, Thread t) {
if (!l.contains(t))
l.add(t);
} private static synchronized void freeIfHardwait(List l, Thread t) {
if (l.contains(t))
l.remove(t);
} //
// Deadlock checking methods
//
// Given a thread, return all locks that are already owned
// Must own class lock prior to calling this method
private static Iterator getAllLocksOwned(Thread t) {
DeadlockDetectingLock current;
ArrayList results = new ArrayList(); Iterator itr = deadlockLocksRegistry.iterator();
while (itr.hasNext()) {
current = (DeadlockDetectingLock) itr.next();
if (current.getOwner() == t)
results.add(current);
}
return results.iterator();
} // Given a lock, return all threads that are hard waiting for the lock
// Must own class lock prior to calling this method
private static Iterator getAllThreadsHardwaiting(DeadlockDetectingLock l) {
return l.hardwaitingThreads.iterator();
} // Check to see if a thread can perform a hard wait on a lock
private static synchronized boolean canThreadWaitOnLock(Thread t,
DeadlockDetectingLock l) {
Iterator locksOwned = getAllLocksOwned(t);
while (locksOwned.hasNext()) {
DeadlockDetectingLock current = (DeadlockDetectingLock) locksOwned
.next(); // Thread can't wait if lock is already owned. This is the end
// condition
// for the recursive algorithm -- as the initial condition should be
// already tested for.
if (current == l)
return false; Iterator waitingThreads = getAllThreadsHardwaiting(current);
while (waitingThreads.hasNext()) {
Thread otherthread = (Thread) waitingThreads.next(); // In order for the thread to safely wait on the lock, it can't
// own any locks that have waiting threads that already owns
// lock. etc. etc. etc. recursively etc.
if (!canThreadWaitOnLock(otherthread, l)) {
return false;
}
}
}
return true;
} //
// Core Constructors
//
public DeadlockDetectingLock() {
this(false, false);
} public DeadlockDetectingLock(boolean fair) {
this(fair, false);
} private boolean debugging; public DeadlockDetectingLock(boolean fair, boolean debug) {
super(fair);
debugging = debug;
registerLock(this);
} //
// Core Methods
//
public void lock() {
// Note: Owner can't change if current thread is owner. It is
// not guaranteed otherwise. Other owners can change due to
// condition variables.
if (isHeldByCurrentThread()) {
if (debugging)
System.out.println("Already Own Lock");
super.lock();
freeIfHardwait(hardwaitingThreads, Thread.currentThread());
return;
} // Note: The wait list must be marked before it is tested because
// there is a race condition between lock() method calls.
markAsHardwait(hardwaitingThreads, Thread.currentThread());
if (canThreadWaitOnLock(Thread.currentThread(), this)) {
if (debugging)
System.out.println("Waiting For Lock");
super.lock();
freeIfHardwait(hardwaitingThreads, Thread.currentThread());
if (debugging)
System.out.println("Got New Lock");
} else {
throw new DeadlockDetectedException("DEADLOCK");
}
} //
// Note: It is debatable whether this is a hard or soft wait. Even if
// interruption is common, we don't know if the interrupting thread
// is also involved in the deadlock. As a compromise, we'll just
// not allow interrupts. This method is disabled.
public void lockInterruptibly() throws InterruptedException {
lock();
} //
// Note: It is not necessary to override the tryLock() methods. These
// methods perform a soft wait -- there is a limit to the wait. It
// not possible to deadlock when locks are not waiting indefinitely.
// // Note 1: Deadlocks are possible with any hard wait -- this includes
// the reacquitition of the lock upon return from an await() method.
// As such, condition variables will mark for the future hard
// wait, prior to releasing the lock.
// Note 2: There is no need to check for deadlock on this end because
// a deadlock can be created whether the condition variable owns the
// lock or is reacquiring it. Since we are marking *before* giving
// up ownership, the deadlock will be detected on the lock() side
// first. It is not possible to create a new deadlock just by releasing
// locks.
public class DeadlockDetectingCondition implements Condition {
Condition embedded; protected DeadlockDetectingCondition(ReentrantLock lock,
Condition embedded) {
this.embedded = embedded;
} // Note: The algorithm can detect a deadlock condition if the thead is
// either waiting for or already owns the lock, or both. This is why
// we have to mark for waiting *before* giving up the lock.
public void await() throws InterruptedException {
try {
markAsHardwait(hardwaitingThreads, Thread.currentThread());
embedded.await();
} finally {
freeIfHardwait(hardwaitingThreads, Thread.currentThread());
}
} public void awaitUninterruptibly() {
markAsHardwait(hardwaitingThreads, Thread.currentThread());
embedded.awaitUninterruptibly();
freeIfHardwait(hardwaitingThreads, Thread.currentThread());
} public long awaitNanos(long nanosTimeout) throws InterruptedException {
try {
markAsHardwait(hardwaitingThreads, Thread.currentThread());
return embedded.awaitNanos(nanosTimeout);
} finally {
freeIfHardwait(hardwaitingThreads, Thread.currentThread());
}
} public boolean await(long time, TimeUnit unit)
throws InterruptedException {
try {
markAsHardwait(hardwaitingThreads, Thread.currentThread());
return embedded.await(time, unit);
} finally {
freeIfHardwait(hardwaitingThreads, Thread.currentThread());
}
} public boolean awaitUntil(Date deadline) throws InterruptedException {
try {
markAsHardwait(hardwaitingThreads, Thread.currentThread());
return embedded.awaitUntil(deadline);
} finally {
freeIfHardwait(hardwaitingThreads, Thread.currentThread());
}
} public void signal() {
embedded.signal();
} public void signalAll() {
embedded.signalAll();
}
} // Return a condition variable that support detection of deadlocks
public Condition newCondition() {
return new DeadlockDetectingCondition(this, super.newCondition());
} //
// Testing routines here
//
// These are very simple tests -- more tests will have to be written
private static Lock a = new DeadlockDetectingLock(false, true); private static Lock b = new DeadlockDetectingLock(false, true); private static Lock c = new DeadlockDetectingLock(false, true); private static Condition wa = a.newCondition(); private static Condition wb = b.newCondition(); private static Condition wc = c.newCondition(); private static void delaySeconds(int seconds) {
try {
Thread.sleep(seconds * 1000);
} catch (InterruptedException ex) {
}
} private static void awaitSeconds(Condition c, int seconds) {
try {
c.await(seconds, TimeUnit.SECONDS);
} catch (InterruptedException ex) {
}
} private static void testOne() {
new Thread(new Runnable() {
public void run() {
System.out.println("thread one grab a");
a.lock();
delaySeconds(2);
System.out.println("thread one grab b");
b.lock();
delaySeconds(2);
a.unlock();
b.unlock();
}
}).start(); new Thread(new Runnable() {
public void run() {
System.out.println("thread two grab b");
b.lock();
delaySeconds(2);
System.out.println("thread two grab a");
a.lock();
delaySeconds(2);
a.unlock();
b.unlock();
}
}).start();
} private static void testTwo() {
new Thread(new Runnable() {
public void run() {
System.out.println("thread one grab a");
a.lock();
delaySeconds(2);
System.out.println("thread one grab b");
b.lock();
delaySeconds(10);
a.unlock();
b.unlock();
}
}).start(); new Thread(new Runnable() {
public void run() {
System.out.println("thread two grab b");
b.lock();
delaySeconds(2);
System.out.println("thread two grab c");
c.lock();
delaySeconds(10);
b.unlock();
c.unlock();
}
}).start(); new Thread(new Runnable() {
public void run() {
System.out.println("thread three grab c");
c.lock();
delaySeconds(4);
System.out.println("thread three grab a");
a.lock();
delaySeconds(10);
c.unlock();
a.unlock();
}
}).start();
} private static void testThree() {
new Thread(new Runnable() {
public void run() {
System.out.println("thread one grab b");
b.lock();
System.out.println("thread one grab a");
a.lock();
delaySeconds(2);
System.out.println("thread one waits on b");
awaitSeconds(wb, 10);
a.unlock();
b.unlock();
}
}).start(); new Thread(new Runnable() {
public void run() {
delaySeconds(1);
System.out.println("thread two grab b");
b.lock();
System.out.println("thread two grab a");
a.lock();
delaySeconds(10);
b.unlock();
c.unlock();
}
}).start(); } public static void main(String args[]) {
int test = 1;
if (args.length > 0)
test = Integer.parseInt(args[0]);
switch (test) {
case 1:
testOne(); // 2 threads deadlocking on grabbing 2 locks
break;
case 2:
testTwo(); // 3 threads deadlocking on grabbing 2 out of 3 locks
break;
case 3:
testThree(); // 2 threads deadlocking on 2 locks with CV wait
break;
default:
System.err.println("usage: java DeadlockDetectingLock [ test# ]");
}
delaySeconds(60);
System.out.println("--- End Program ---");
System.exit(0);
}
} class DeadlockDetectedException extends RuntimeException { public DeadlockDetectedException(String s) {
super(s);
}
}

Deadlock Detecting--转的更多相关文章

  1. MemcacheQ 安装与使用

    MemcacheQ 是一个基于 MemcacheDB 的消息队列服务器.官网地址:http://memcachedb.org/memcacheq/ 特点: 1.简单易用. 2.处理速度快. 3.可创建 ...

  2. MemcacheQ安装及使用

    一.MemcacheQ安装记录1.安装libevent查看是否已经安装了libeventrpm -qa|grep libevent如果没有安装使用yum安装yum install libevent l ...

  3. 持久化消息队列memcacheq的安装配置

    MemcacheQ 是一个基于 MemcacheDB 的消息队列服务器. 一.memcacheq介绍 特性: 1.简单易用 2.处理速度快 3.多条队列 4.并发性能好 5.与memcache的协议兼 ...

  4. Troubleshooting "Global Enqueue Services Deadlock detected" (Doc ID 1443482.1)

    In this Document   _afrLoop=1021148011984950&id=1443482.1&displayIndex=1&_afrWindowMode= ...

  5. 又踩.NET Core的坑:在同步方法中调用异步方法Wait时发生死锁(deadlock)

    之前在将 Memcached 客户端 EnyimMemcached 迁移 .NET Core 时被这个“坑”坑的刻骨铭心(详见以下链接),当时以为只是在构造函数中调用异步方法(注:这里的异步方法都是指 ...

  6. 【转】Deadlock的一些总结(死锁分析及处理)

    1.1.1 摘要 在系统设计过程中,系统的稳定性.响应速度和读写速度至关重要,就像12306.cn那样,当然我们可以通过提高系统并发能力来提高系统性能总体性能,但在并发作用下也会出现一些问题,例如死锁 ...

  7. 论文阅读(Xiang Bai——【CVPR2012】Detecting Texts of Arbitrary Orientations in Natural Images)

    Xiang Bai--[CVPR2012]Detecting Texts of Arbitrary Orientations in Natural Images 目录 作者和相关链接 方法概括 方法细 ...

  8. Oracle Deadlock / 死锁 处理

    Get the directory of alert log sqlplus / as sysdba show parameters udmp SQL> show parameters dump ...

  9. [No00003D]操作系统Operating Systems信号量的代码实现Coding Semaphore &死锁处理Deadlock

    操作系统Operating Systems信号量的代码实现Coding Semaphore &死锁处理Deadlock 可以操刀了—从纸上到实际 从Linux 0.11 那里学点东西… 读磁盘 ...

随机推荐

  1. STM32之通用定时器

    广大的互联网的大家早上中午晚上..又好..没错了..我又来了..写博客不是定时的..为什么我要提写博客不是定时的呢??聪明的人又猜到我要说什么了吧.有前途.其实我还是第一次听到定时器有通用和高级之分的 ...

  2. JS键盘KEYCODE值参考

    keycode 1 = 鼠标左键keycode 2 = 鼠标右键keycode 3 = Cancelkeycode 4 = 鼠标中键keycode 8 = BackSpace keycode 9 = ...

  3. QComboBox的activated与currentIndexChanged的区别

    void activated ( int index ) void activated ( const QString & text ) 信号activated是只要单击选择框即使所选内容选择 ...

  4. 新版startssl 免费SSL证书申请 (实测 笔记 https http2 必要条件)

    简单说明: 目前多个大型网站都实现全站HTTPS,而SSL证书是实现HTTPS的必要条件之一. StartSSL是StartCom公司旗下的.提供免费SSL证书服务并且被主流浏览器支持的免费SSL.包 ...

  5. Tomcat 配置续

    为了便于tomcat升级 不用频繁重新修改相关配置文件 不用重新部署原有项目 新建目录将tomcat安装目录中的conf,log,webapp,work文件夹复制到里面,然后将系统变量里的CATALI ...

  6. jQuery的几种简单实用效果

    许久未分享博客,或许已生疏. 闲来无事, 分享几个jQuery简单实用的效果案例 不喜勿喷... 1.页面常用的返回顶部 <!DOCTYPE html> <html lang=&qu ...

  7. 一鼓作气 博客--第五篇 note5

    一.迭代器 二.装饰器 三.生成器 1.生成列表的方式有几种 2.把列表每个数都加1 2.1 data =[1,2,3] for i in map(lambda x:x+1,data):print(i ...

  8. doduicms 手机和pc同步 链接处理

    sitem.aspx 加上 Session.Add("ismobile", "1"); string mobile = HttpContext.Current. ...

  9. jquery的each

    each()方法能使DOM循环结构简洁,不容易出错.each()函数封装了十分强大的循环功能,使用也很方便,它可以循环一维数组.多维数组.DOM, JSON 等等在javaScript开发过程中使用$ ...

  10. Nodejs之MEAN栈开发(二)----视图与模型

    上一节做了对Express做了简单的介绍,提出了controller,介绍了路由.这一节将重点放到视图和模型上,完成几个静态页面并部署到heroku上. 导航 前端布局使用bootstrap,从官网下 ...