线程同步 · 死锁

线程同步

这是一个非常经典的例子,银行取钱。
关键是:public synchronized void run() 用关键字 synchronized

package multithread;

public class TestBank {
    public static void main(String[] args) {
        new TestBank().run();
    }


    public void run(){
        Family f = new Family();
        new Thread(f,"丈夫").start();
        new Thread(f,"妻子").start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        f.show();

    }


    class Family implements Runnable{
        private int saveMoney;
        private int getMoney;
        private int curMoney;
        private int times = 0;

        public Family(){
            saveMoney = 5000;
            getMoney = 2000;
            curMoney = 0;
        }

        @Override
        public synchronized void run() {
            System.out.println(Thread.currentThread().getName()+"取了:"+getMoney+"元");
            curMoney += getMoney;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            int temp = saveMoney - getMoney;
            saveMoney = temp;
            times = times+1 ;
        }

        public void show(){
            System.out.println("银行还有:"+saveMoney+" 家中还有:"+curMoney);
        }

    }
}

死锁

引起原因:synchronized 的嵌套

package multithread;


public class TestDeadLock {

    public static void main(String[] args) {
        new TestDeadLock().run();
    }

    public void run(){
        MyThread mt = new MyThread();
        new Thread(mt,"张三").start();
        new Thread(mt,"李四").start();
    }

    class MyThread implements Runnable{
        private int index = 0;
        private Object k1 = new Object();
        private Object k2 = new Object();
        private boolean flag = true;

        @Override
        public void run() {
            if(flag){
                flag = false;
                synchronized (k1) {
                    System.out.println(Thread.currentThread().getName()+":k1");
                    try {
                        Thread.sleep(100);
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                    synchronized (k2) {
                        System.out.println(Thread.currentThread().getName()+":k2");
                    }
                }

            }else{
                flag = true;
                synchronized (k2) {
                    System.out.println(Thread.currentThread().getName()+":k2");
                    try {
                        Thread.sleep(100);
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                    synchronized (k1) {
                        System.out.println(Thread.currentThread().getName()+":k1");
                    }
                }
            }
        }

    }

}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/milliemeter/article/details/46777071
个人分类: 多线程
上一篇多线程基础 定义线程类 · 启动线程 · 停止线程
下一篇生产者与消费者模型 wait() · notify() 方法的使用
想对作者说点什么? 我来说一句

线程同步---死锁

confirmAname confirmAname

2014-03-31 20:54:34

阅读数:325

JAVA_线程同步死锁

2012年08月16日 32KB 下载

没有更多推荐了,返回首页

关闭
关闭