線程同步 · 死鎖

線程同步

這是一個非常經典的例子,銀行取錢。
關鍵是: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");
                    }
                }
            }
        }

    }

}
發佈了33 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章