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

多线程——死锁

时间:2018-10-27 17:43:42      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:over   imp   code   ==   情况   thread   多线程   sts   str   

多线程死锁的简单使用:

public class MyLock{
    public static final Object locka=new Object();
    public static final Object lockb=new Object();
    public static final Object lockc=new Object();
    public static final Object lockd=new Object();
}
package day_12_01_Thread;

/**
 * 死锁的情况
 * 
 * @author Administrator
 *
 */
public class TestSynchronized implements Runnable {
    public static void main(String[] args) {
        TestSynchronized s = new TestSynchronized();
        Thread t1 = new Thread(s);
        Thread t2 = new Thread(s);

        t1.start();
        t2.start();
    }

    @Override
    public void run() {
        int i = 0;
        while (true) {
            if (i % 2 == 0) {
                synchronized (MyLock.locka) {
                    System.out.println("if.......locka");
                    synchronized (MyLock.lockb) {
                        System.out.println("if ...........lockb");
                    }
                }
            } else {
                synchronized (MyLock.lockb) {
                    System.out.println("else..........lockb");
                    synchronized (MyLock.locka) {
                        System.out.println("else..........locka");
                    }
                }
            }

            i++;
        }
    }
}

 

多线程——死锁

标签:over   imp   code   ==   情况   thread   多线程   sts   str   

原文地址:https://www.cnblogs.com/whx20100101/p/9862348.html

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