死锁的演示

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 第一个线程获取local锁后,偿试获取other锁,如果失败,则等待5秒重试
 * 同样第二个线程获取other锁后,偿试获取local锁,如果失败,则放弃等待5秒重试
 * User: sunlong
 * Date: 13-10-18
 * Time: 下午5:10
 */
public class DeadLock {
    public static void main(String[] args){
        final Update a = new Update("this is a");
        final Update b = new Update("this is b");

        final App2 local = new App2("app1");
        final App2 other = new App2("app2");

        new Thread(new Runnable() {
            @Override
            public void run() {
                local.update(a, other);
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                other.update(b, local);
            }
        }).start();
    }

}

class Update{
    private String text;
    public Update(String text){
        this.text = text;
    }

    public String getText(){
        return this.text;
    }
}

/**
 * 这个是传统的syschronized方式演示死锁
 */
class App{
    private final String indent;

    public App(String indent){
        this.indent = indent;
    }

    public synchronized void update(Update update, App backup){
        System.out.println(indent + ": recvd:" + update.getText() + "; backup:" + backup.getIndent());
        /*try{
            TimeUnit.SECONDS.sleep(5);
        }catch (Exception e){

        }*/

        backup.confirmUpdate(this, update);
    }

    private synchronized void confirmUpdate(App backup, Update update){
        System.out.println(indent + ": recvd:" + update.getText() + "; backup:" + backup.getIndent());
    }

    String getIndent() {
        return indent;
    }
}

/**
 * 用lock重写
 */
class App2{
    private final String indent;

    private final ReentrantLock lock = new ReentrantLock();

    public App2(String indent){
        this.indent = indent;
    }

    public void update(Update update, App2 backup){
        boolean acquired = false;
        boolean done = false;
        while (!done){
            System.out.println("--------------------------------");
            try {
                acquired = lock.tryLock(5, TimeUnit.MILLISECONDS);
                if(acquired){
                    System.out.println(indent + ": recvd:" + update.getText() + "; backup:" + backup.getIndent());
                    done = backup.confirmUpdate(this, update);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                if(acquired){
                    lock.unlock();
                }
            }

            if(!done){
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private boolean confirmUpdate(App2 backup, Update update){
        boolean acquired = false;

        try{
            acquired = lock.tryLock(5, TimeUnit.MILLISECONDS);
            if(acquired){
                System.out.println(indent + ": recvd confirmed:" + update.getText() + "; backup:" + backup.getIndent());
                return true;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if(acquired){
                lock.unlock();
            }
        }
        return false;
    }

    String getIndent() {
        return indent;
    }
}

猜你喜欢

转载自sunlong.iteye.com/blog/1961589
今日推荐