MyException - 我的异常网
当前位置:我的异常网» J2SE » 这段代码实现不了死锁吗?该怎么解决

这段代码实现不了死锁吗?该怎么解决

www.MyException.Cn  网友分享于:2013-12-31  浏览:5次
这段代码实现不了死锁吗?
Java code

public class Deadlock {
    private String a="";
    private String b="";
    private int n=1;
    
    public void write(){
        synchronized (a) {
              try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            synchronized (b) {
                
                System.out.println("第" +
                        n+"次被写入");
                n++;
                
                
            }
            
        }
    }
    public void read(){
        synchronized (b) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            synchronized (a) {
                System.out.println("第" +
                        n+"次被读取");
                n++;
                
            }
            
        }
    }

Java code
public class MyDeadlock implements Runnable{
    private Deadlock dl;

    public MyDeadlock(Deadlock dl) {
        this.dl = dl;
    }

    @Override
    public void run() {
        for(int i=0;i<100;i++){
            dl.write();
        
        dl.read();
        }
    }
    

Java code
public class TestDeadlock {
    public static void main(String[] args) {
        Deadlock deadlock=new Deadlock();
        MyDeadlock md=new MyDeadlock(deadlock);
        Thread t1=new Thread(md);
        Thread t2=new Thread(md);
        t1.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        t2.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }



能sleep的地方都sleep了,就是不死锁,是不是什么地方有问题?

------解决方案--------------------
死锁是a占用b资源,b又占用a资源。先理解死锁的概念吧
Java code
package com.zzx.study.thread;

public class TestDeadLock implements Runnable {
    public int flag = 1;
    static Object o1 = new Object(), o2 = new Object();
    public void run() {
System.out.println("flag=" + flag);
        if(flag == 1) {
            synchronized(o1) {
                try {
                    Thread.sleep(500);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                synchronized(o2) {
                    System.out.println("1");    
                }
            }
        }
        if(flag == 0) {
            synchronized(o2) {
                try {
                    Thread.sleep(500);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                synchronized(o1) {
                    System.out.println("0");
                }
            }
        }
    }    
    
    public static void main(String[] args) {
        TestDeadLock td1 = new TestDeadLock();
        TestDeadLock td2 = new TestDeadLock();
        td1.flag = 1;
        td2.flag = 0;
        Thread t1 = new Thread(td1);
        Thread t2 = new Thread(td2);
        t1.start();
        t2.start();
        
    }
}

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有