码迷,mamicode.com
首页 > 编程语言 > 详细

2.2.12多线程的死锁

时间:2017-12-09 14:56:15      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:lis   实现   col   执行   代码结构   经典   monit   wait   现象   

java线程死锁是一个经典的多线程问题,因为不同的线程都在等待根本不可能释放的锁,从而导致所有的任务无法继续完成,在多线程技术中,死锁是必须避免的,因为会造成线程的假死。

package com.cky.thread;

/**
 * Created by edison on 2017/12/9.
 */
public class DeadThread implements  Runnable{
    public String username;
    public Object object1 = new Object();
    public Object object2 = new Object();
    public void setFlag(String username) {
        this.username = username;
    }
    @Override
    public void run() {
        if (username.equals("a")) {
            synchronized (object1) {
                try {
                    System.out.println("username="+username);
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (object2) {
                    System.out.println("按lock1-》lock2代码顺序执行了");
                }
            }



        }

        if (username.equals("b")) {
            synchronized (object2) {
                try {
                    System.out.println("username="+username);
                    Thread.sleep(2000);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (object1) {
                    System.out.println("按lock2-》lock1代码顺序执行了");
                }
            }
        }

    }
}
package com.cky.test;

import com.cky.thread.DeadThread;

/**
 * Created by edison on 2017/12/9.
 */
public class Run {
    public static void main(String[] args) {
        try {
            DeadThread t1 = new DeadThread();
            t1.setFlag("a");
            Thread thread1 = new Thread(t1);
            thread1.start();
            Thread.sleep(100);
            t1.setFlag("b");
            Thread t2 = new Thread(t1);
            t2.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
}
username=a
username=b

程序运行结果如上。

 可以用jdk自带的工具来监测是否有死锁现象

C:\Users\edison>cd C:\itsoft\jdk\bin

C:\itsoft\jdk\bin>jps
18016 Launcher
14392 Jps
20696
4936 AppMain
C:\itsoft\jdk\bin>jstack -l 4936
2017-12-09 13:49:50
Full thread dump Java HotSpot(TM) Client VM (25.144-b01 mixed mode, sharing):
=============================
"Thread-1":
  waiting to lock monitor 0x00d7ad94 (object 0x04d01230, a java.lang.Object),
  which is held by "Thread-0"
"Thread-0":
  waiting to lock monitor 0x00d793c4 (object 0x04d01238, a java.lang.Object),
  which is held by "Thread-1"

Java stack information for the threads listed above:
===================================================
"Thread-1":
        at com.cky.thread.DeadThread.run(DeadThread.java:44)
        - waiting to lock <0x04d01230> (a java.lang.Object)
        - locked <0x04d01238> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:748)
"Thread-0":
        at com.cky.thread.DeadThread.run(DeadThread.java:25)
        - waiting to lock <0x04d01238> (a java.lang.Object)
        - locked <0x04d01230> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:748)

Found 1 deadlock.

监测出现死锁

死锁是程序设计的bug,在程序设计时就要避免双方互相持有对方锁的情况,需要说明的是,本实验使用synchronized嵌套的代码结构来实现死锁,

其实不用嵌套的synchronized代码结构也会出现死锁,与是否嵌套无任何关系,不要被代码结构所误导,只要互相等待双方释放就有可能出现死锁。

2.2.12多线程的死锁

标签:lis   实现   col   执行   代码结构   经典   monit   wait   现象   

原文地址:http://www.cnblogs.com/edison20161121/p/8011168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!