-
java 线程死锁的检测
- import java.util.concurrent.CountDownLatch;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- 将夜www.jiangyea.com
-
-
-
- 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();
- }
- }
- }
- }
- }
-
相关阅读:
打造一款便携版的Sublime Text
git stash命令使用手册
Java List 转 String
myeclipse中java文件头注释格式设置
IntelliJ IDEA详细配置和使用教程-字体、编码和基本设置
Android Studio添加文件注释头模板?
Windows + Ubuntu下JDK与adb/android环境变量配置完整教程
android studio gradle dependencies 包存放在哪儿?
用Gradle命令行编译Android工程
十分钟玩转 jQuery、实例大全
-
原文地址:https://www.cnblogs.com/jiangye/p/3319148.html
Copyright © 2020-2023
润新知