多線程學習筆記三--------------多線程死結

來源:互聯網
上載者:User

標籤:

多線程死結:
  死結的常見情形之一:同步的嵌套。

public class DeadLock {        public static void main(String[] args) throws InterruptedException {            Customer cus = new Customer();            Thread t1 = new Thread(cus);            Thread t2 = new Thread(cus);            t1.start();            Thread.sleep(5);            cus.flag = false;            t2.start();        }    }    class Bank2{        private double sumMoney;                public double getSumMoney() {            return sumMoney;        }            public void setSumMoney(double sumMoney) {            this.sumMoney = sumMoney;        }            public void add(double money){            sumMoney += money;            System.out.println(Thread.currentThread().getName()+"save:"+money+"   Bank2 has "+sumMoney);        }    }    class Customer implements Runnable{        Bank2 b = new Bank2();        public boolean flag = true;        Object obj = new Object();        @Override        public void run() {            if(flag){                while(true){                    if(b.getSumMoney() < 100000){                        synchronized(obj){                            this.Operation();                        }                    }else{break;}                }            }else{                while(true){                    if(b.getSumMoney() < 100000){                        this.Operation();                    }else{break;}                }            }                    }        public synchronized void Operation(){            synchronized (obj) {                b.add(1000);                System.out.println("======function=====");;            }        }    }

運行結果:
======function=====
Thread-1save:1000.0 Bank2 has 34000.0
======function=====
Thread-1save:1000.0 Bank2 has 35000.0
======function=====
Thread-1save:1000.0 Bank2 has 36000.0
======function=====
Thread-1save:1000.0 Bank2 has 37000.0
======function=====
Thread-1save:1000.0 Bank2 has 38000.0
======function=====
到38000時候發生了死結。
  死結小執行個體:(面試時候用)

  

public class DeadLockTest {        public static void main(String[] args) throws InterruptedException {            MyThread mt1 = new MyThread();            Thread t1 = new Thread(mt1);            Thread t2 = new Thread(mt1);            t1.start();            Thread.sleep(1);            mt1.flag = false;            t2.start();        }    }        class MyThread implements Runnable{        public boolean flag = true ;        @Override        public void run() {            while(true){                if(flag){                    synchronized(LockObject.obja){                        try {                            Thread.sleep(10);                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                        System.out.println(Thread.currentThread().getName()+"if===obja");                        synchronized(LockObject.objb){                            System.out.println(Thread.currentThread().getName()+"if===objb");                            }                    }                }else{                    synchronized(LockObject.objb){                        try {                            Thread.sleep(10);                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                        System.out.println(Thread.currentThread().getName()+"else===obja");                        synchronized(LockObject.obja){                            System.out.println(Thread.currentThread().getName()+"else===objb");                            }                    }                }            }        }            }    class LockObject{        public static final Object obja = new Object();        public static final Object objb = new Object();    }    

 

多線程學習筆記三--------------多線程死結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.