java multithreading - deadlock and solutions

Deadlock:
Excessive synchronization with each other does not cause the release of resources to wait too much, generally occurs in
lock synchronization hold multiple objects

snchronized lock objects simultaneously, another snchronized lock of the object can not be
avoided in a code block, a lock while holding a plurality of objects

Deadlock:

public class tt {

public static void main(String[]args)
{
    markup m1=new markup(1,"me");
    markup m2 =new markup(2,"she");
    m1.start();
    m2.start();
}
}

//口红
class lipstick{

}

//镜子
class mirror{

}

//化妆
class markup extends Thread{
//加静态表示一份,不管创建几个对象都是一份
static lipstick lip=new lipstick();
static mirror mir=new mirror();

//选择
int choice;
String girl;

public markup(int choice,String girl)
{
    this.choice=choice;
    this.girl=girl;
}

public void run(){

    mark();
}
//相互持有对方的对象锁-->可能造成死锁
//加等待时间是为了让另一个线程锁相同对象的时候,该线程
//锁的相同对象还没有结束
//在这个死锁中,第一个线程内的mir要等第二个已经开始的mir锁结束才能执行
//而第二个线程内的mir结束需要等第一个线程内的lip解锁才行
private void mark()
{
    if(choice==0)
    {
        synchronized(lip)
        {
            System.out.println(this.girl+"获得口红");
            //一秒后想要镜子
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            synchronized(mir)
            {
                System.out.println(this.girl+"获得镜子");
            } 

        }

    }else
    {
        synchronized(mir)
        {
            System.out.println(this.girl+"获得镜子");
            //2秒后想要口红
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        synchronized(lip)
            {
                System.out.println(this.girl+"获得口红");
            } 
        }

    }
}
}

solve:

public class tt {

public static void main(String[]args)
{
    markup m1=new markup(1,"me");
    markup m2 =new markup(2,"she");
    m1.start();
    m2.start();
}
}

//口红
class lipstick{

}

//镜子
class mirror{

}

//化妆
class markup extends Thread{
//加静态表示一份,不管创建几个对象都是一份
static lipstick lip=new lipstick();
static mirror mir=new mirror();

//选择
int choice;
String girl;

public markup(int choice,String girl)
{
    this.choice=choice;
    this.girl=girl;
}

public void run(){

    mark();
}
{
    if(choice==0)
    {
        synchronized(lip)
        {
            System.out.println(this.girl+"获得口红");
            //一秒后想要镜子
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        synchronized(mir)
        {
            System.out.println(this.girl+"获得镜子");
        } 

    }else
    {
        synchronized(mir)
        {
            System.out.println(this.girl+"获得镜子");
            //2秒后想要口红
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        synchronized(lip)
        {
            System.out.println(this.girl+"获得口红");
        } 

    }
}
}

Guess you like

Origin blog.51cto.com/14437184/2429806