-
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();
- }
- }
- }
- }
- }
-
相关阅读:
NPOI操作 EXCEL文件
mysql 临时变量,临时表
ReSharper制作Template帮助我们快速输入代码
临时表的实际运用
如何避免项目重复循环引用
mysql 5.1以上版本数据库 使用定时器功能(Event)
Fedora12下装SMPlayer
Fedora12启动时激活网卡并将firefox不设置为脱机工作
设置yum
Fedora 有用更新源上海交大的yum源
-
原文地址:https://www.cnblogs.com/jiangye/p/3319148.html
Copyright © 2020-2023
润新知