Java多線程和死結

來源:互聯網
上載者:User

標籤:rate   ack   start   getname   nts   strong   adl   代碼塊   err   

一 、多線程:

售票視窗簡單一實例:

public class SaleTicket {    public static class Sale implements Runnable{        private int ticket=10;//        @Override//        public void run() {//            while(true){//                synchronized(this){         //同步代碼塊   synchronized(this)鎖著的是對象 //                    if(ticket>0){//                        try {//                            Thread.sleep(500);//                        } catch (InterruptedException e) {//                            // TODO Auto-generated catch block//                            e.printStackTrace();//                        }//                        System.out.println(Thread.currentThread().getName()+"賣完一張,車票剩餘:"+ticket--);//                    }else break;//                }//            }//            //        }        @Override        public void run() {            for(int i=0;i<20;i++){                tell();            }        }                public synchronized void tell(){    //同步方法            if(ticket>0){                try {                    Thread.sleep(500);                } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                System.out.println(Thread.currentThread().getName()+"賣完一張,車票剩餘:"+ticket--);            }        }            }    public static void main(String[] args) {        Sale s = new Sale();        Thread t1 = new Thread(s, "售票員1");        Thread t2 = new Thread(s, "售票員2");        Thread t3 = new Thread(s, "售票員3");        t1.start();        t2.start();        t3.start();    }}

運用結果:

售票員1賣完一張,車票剩餘:10
售票員1賣完一張,車票剩餘:9
售票員1賣完一張,車票剩餘:8
售票員1賣完一張,車票剩餘:7
售票員3賣完一張,車票剩餘:6
售票員1賣完一張,車票剩餘:5
售票員2賣完一張,車票剩餘:4
售票員2賣完一張,車票剩餘:3
售票員3賣完一張,車票剩餘:2
售票員3賣完一張,車票剩餘:1

 

二、線程死結:

線程的同步化可能會造成死結,死結發生在兩個線程相互持有對方正在等待的東西(實際是兩個線程共用的東西)。只要有兩個線程和兩個對象就可能產生死結

 

/** *  * 一個簡單的死結類 * 當DeadLock類的對象flag==1時(td1),先鎖定o1,睡眠500毫秒 * 而td1在睡眠的時候另一個flag==0的對象(td2)線程啟動,先鎖定o2,睡眠500毫秒 * td1睡眠結束後需要鎖定o2才能繼續執行,而此時o2已被td2鎖定; * td2睡眠結束後需要鎖定o1才能繼續執行,而此時o1已被td1鎖定; * td1、td2相互等待,都需要得到對方鎖定資源才能繼續執行,從而死結。 */ public class DeadLock implements Runnable{    public int flag = 1;    //靜態對象是類的所有對象共用的      private static Object o1 = new Object(),o2 = new Object();        @Override    public void run() {        System.out.println("flag=" + flag);          if(flag == 1){            synchronized (o1) {                try {                    Thread.sleep(500);                } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                                synchronized(o2){                    System.out.println("1");                }            }                    }                if(flag == 0){            synchronized(o2){                try {                    Thread.sleep(500);                } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                                synchronized(o1){                    System.out.println("0");                }            }                    }    }    public static void main(String[] args) {        DeadLock td1 = new DeadLock();        DeadLock td2 = new DeadLock();        td1.flag = 1;        td2.flag = 0;        new Thread(td1,"td1").start();        new Thread(td2,"td2").start();    }    }

 

運行結果:

flag=1
flag=0

結果產生了死結

 

Java多線程和死結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.