java 线程死锁的检测
- import java.util.concurrent.CountDownLatch;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- /**
- * Hello world!
- */
- public class App {
- public static void main(String[] args) throws InterruptedException {
- System.out.println("Hello World!");
- ExecutorService executorService = Executors.newFixedThreadPool(2);
- byte[] i = new byte[0];
- byte[] j = new byte[0];
- final CountDownLatch countDownLatch = new CountDownLatch(2);
- executorService.execute(new DeadThread1(i, j,countDownLatch));
- executorService.execute(new DeadThread2(i, j,countDownLatch));
- countDownLatch.await();
- System.out.println("done !!!");
- }
- public static class DeadThread1 implements Runnable {
- private byte[] i;
- private byte[] j;
- private CountDownLatch countDownLatch;
- public DeadThread1(byte[] i, byte[] j, CountDownLatch countDownLatch) {
- this.i = i;
- this.j = j;
- this.countDownLatch = countDownLatch;
- }
- @Override
- public void run() {
- synchronized (i) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- synchronized (j) {
- System.out.println(Thread.currentThread().getName() + "is running!!");
- countDownLatch.countDown();
- }
- }
- }
- }
- public static class DeadThread2 implements Runnable {
- private byte[] i;
- private byte[] j;
- private CountDownLatch countDownLatch;
- public DeadThread2(byte[] i, byte[] j, CountDownLatch countDownLatch) {
- this.i = i;
- this.j = j;
- this.countDownLatch = countDownLatch;
- }
- @Override
- public void run() {
- synchronized (j) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- synchronized (i) {
- System.out.println(Thread.currentThread().getName() + "is running!!");
- countDownLatch.countDown();
- }
- }
- }
- }
- }
通过jps找到当前进程号:
- guohaozhao116008@GUOHAOZHAO11600 /d
- $ jps
- 7448 RemoteMavenServer
- 6600 JConsole
- 6340 Jps
- 6272
- 7268 AppMain
通过jstack查看堆栈信息:
- guohaozhao116008@GUOHAOZHAO11600 /d
- $ jstack
- Usage:
- jstack [-l] <pid>
- (to connect to running process)
- jstack -F [-m] [-l] <pid>
- (to connect to a hung process)
- jstack [-m] [-l] <executable> <core>
- (to connect to a core file)
- jstack [-m] [-l] [server_id@]<remote server IP or hostname>
- (to connect to a remote debug server)
- Options:
- -F to force a thread dump. Use when jstack <pid> does not respond (process is hung)
- -m to print both java and native frames (mixed mode)
- -l long listing. Prints additional information about locks
- -h or -help to print this help message
- guohaozhao116008@GUOHAOZHAO11600 /d
- $ jps
- 7448 RemoteMavenServer
- 6600 JConsole
- 6340 Jps
- 6272
- 7268 AppMain
- guohaozhao116008@GUOHAOZHAO11600 /d
- $ jstack -l 7268
- 2013-05-30 18:36:41
- Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.14-b01 mixed mode):
- Found one Java-level deadlock:
- =============================
- "pool-1-thread-2":
- waiting to lock monitor 0x000000000677c208 (object 0x00000000eafc3e18, a [B),
- which is held by "pool-1-thread-1"
- "pool-1-thread-1":
- waiting to lock monitor 0x0000000006771be8 (object 0x00000000eafc3e28, a [B),
- which is held by "pool-1-thread-2"
- Java stack information for the threads listed above:
- ===================================================
- "pool-1-thread-2":
- at com.sohu.suc.App$DeadThread2.run(App.java:74)
- - waiting to lock <0x00000000eafc3e18> (a [B)
- - locked <0x00000000eafc3e28> (a [B)
- at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
- at java.lang.Thread.run(Thread.java:662)
- "pool-1-thread-1":
- at com.sohu.suc.App$DeadThread1.run(App.java:45)
- - waiting to lock <0x00000000eafc3e28> (a [B)
- - locked <0x00000000eafc3e18> (a [B)
- at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
- at java.lang.Thread.run(Thread.java:662)
- Found 1 deadlock.
可以看到:
这里发生了死锁。
使用图形工具 jconsole 同样可以检测到:

java 线程死锁的检测的更多相关文章
- Java线程死锁查看分析方法
如何查看是否有Java线程死锁?下面介绍两种方法. 一.Jconsole Jconsole是JDK自带的图形化界面工具,使用JDK给我们的的工具JConsole,可以通过打开cmd然后输 ...
- Java——线程死锁问题
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- 55行代码实现Java线程死锁
死锁是Java多线程的重要概念之一,也经常出现在各大公司的笔试面试之中.那么如何创造出一个简单的死锁情况?请看代码: class Test implements Runnable { boolean ...
- 一个Java线程死锁的例子
package com.lk.B; public class Test4 { private static final Object o1 = new Object(); private static ...
- 【!Important】Java线程死锁查看分析方法
一.Jconsole Jconsole是JDK自带的图形化界面工具,使用JDK给我们提过的工具JConsole,可以通过cmd打开命令框然后输入Jconsole打开图形工具 然后点击检测死锁就可以查看 ...
- JAVA线程死锁
文件名:DeadThreadByExtend.java 注: 1.起线程的时候用的是start方法,run方法也可以调用,但是仅仅相当于普通调用,在当前线程内执行. 2.synchronized 不能 ...
- 诊断Java线程死锁
比如我们有运行这样一个程序: 了解多线程的小伙版都知道,这段代码不会有打印结果,因为发生了死锁.我们在服务器上运行试试,没有输出,对应的进程是 32752. 使用 “jstack 32752”排查,后 ...
- Java 多线程 死锁 隐性死锁 数据竞争 恶性数据竞争 错误解决深入分析 全方向举例
在几乎所有编程语言中,由于多线程引发的错误都有着难以再现的特点,程序的死锁或其它多线程错误可能只在某些特殊的情形下才出现,或在不同的VM上运行同一个程序时错误表现不同.因此,在编写多线程程序时,事先认 ...
- Atitit.线程 死锁 跑飞 的检测与自动解除 与手动解除死锁 java c# .net php javascript.
Atitit.线程 死锁 跑飞 的检测与自动解除 与手动解除死锁 java c# .net php javascript. 1. 现象::主程序卡住无反应,多行任务不往下执行 1 2. 原因::使用j ...
随机推荐
- JavaScript表格搜索高亮功能模拟
在网页表格中模拟excle的搜索高亮显示功能.当在搜索框中输入需要的姓名时,若表格中存在对应的数据,则该表格背景色变为黄色. 下面为表的HTML源码: <!doctype html> &l ...
- El和标准标签
EL表达式针对于四大作用域:application,session,request,pagecontext(作用域由大倒小)${作用域获取内容的名字}是根据作用域最小的取,指定作用域${session ...
- Cracking the Coding Interview 8.7
Given a infinite number of quarters(25cents), dimens(10cents), nickels(5cents) and pennies(1cent), w ...
- BZOJ 4753 二分+树形DP
思路: 先二分答案 f[x][j]表示在x的子树里选j个点 f[x][j+k]=max(f[x][j+k],f[x][j]+f[v[i]][k]); 初始化 x!=0 -> f[x][1]=p[ ...
- Run as ant build每次都执行两次-问题解决
在Eclipse里面,运行ant,整个测试流程总是执行两遍,其几天试了下在DOS命令行直接调用ant, 结果发现只执行了一次,并且内存消耗好像也没那么大了,估计是eclipse自己的问题.问题解决了, ...
- strcpy自实现
为了避免strcpy源串覆盖问题(P220),自实现strcpy. #include <stdio.h> #include <string.h> #include <as ...
- bootstrap-paginator基于bootstrap的分页插件
bootstrap-paginator基于bootstrap的分页插件 GitHub 官网地址:https://github.com/lyonlai/bootstrap-paginator 步骤 引包 ...
- js视频学习笔记1
1:数组赋值的个数长度定义无效,第4个存储的数还是能原封不动打印出来. js的数组是内部有一个变量名叫0,它的值是1,有一变量名叫1,它的值是2.是这样表示的 2:js是弱类型语言,没有var标识符, ...
- Linux系统下通过命令行对mysql数据进行备份和还原
一.备份 1.进入mysql目录 cd /var/lib/mysql (进入mysql目录,根据安装情况会有差别) 2.备份 mysqldump -u root -p密码 数据库名 数据表名 > ...
- 数据库 'tempdb' 的事务日志已满。若要查明无法重用日志中的空间的原因
最常的做法: --1.清空日志 DUMP TRANSACTION tempdb WITH NO_LOG --2.截断事务日志: BACKUP LOG tempdb WITH NO_LOG --3.收缩 ...