java多線程實(shí)現(xiàn)火車售票系統(tǒng) 以及java中的同步的實(shí)現(xiàn) 同步塊 和同步方法同時(shí) 同步_第1頁
java多線程實(shí)現(xiàn)火車售票系統(tǒng) 以及java中的同步的實(shí)現(xiàn) 同步塊 和同步方法同時(shí) 同步_第2頁
java多線程實(shí)現(xiàn)火車售票系統(tǒng) 以及java中的同步的實(shí)現(xiàn) 同步塊 和同步方法同時(shí) 同步_第3頁
java多線程實(shí)現(xiàn)火車售票系統(tǒng) 以及java中的同步的實(shí)現(xiàn) 同步塊 和同步方法同時(shí) 同步_第4頁
java多線程實(shí)現(xiàn)火車售票系統(tǒng) 以及java中的同步的實(shí)現(xiàn) 同步塊 和同步方法同時(shí) 同步_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、java多線程實(shí)現(xiàn)火車售票系統(tǒng) 以及java中的同步的實(shí)現(xiàn) 同步塊 和同步方法同時(shí) 同步   /*利用Java 多線程模擬火車站售票系統(tǒng)  了解java中的同步class  Test  public  static void main(String args)       SellThread st=new SellThread(); /創(chuàng)建一個(gè)實(shí)現(xiàn)了implements接口的對象    new Thread(st).start();   /啟動(dòng)

2、四個(gè)Thread     new Thread(st).start();    new Thread(st).start();    new Thread(st).start();     class SellThread   implements Runnable   int index =100;    public void run()         

3、;    while(true)                 if(index>0)                    try           

4、0;        Thread.sleep(10);  /函數(shù)拋出異常我們要進(jìn)行異常捕獲 休眠之后 所有Thread等待在這里                    catch(Exception e)             

5、          e.printStackTrace();                    System.out.println(Thread.currentThread().getName()+"sell tickets "+ index-);     

6、60;                    */  /*上面的代碼 不能保證同步 即每次只允許一個(gè) THREAD訪問 資源  我們要對這個(gè)臨界區(qū)進(jìn)行保護(hù)   每個(gè)對象都有一個(gè)監(jiān)視器 通過檢測對象是否被枷鎖 從而判斷是否進(jìn)入臨界區(qū)  同步快監(jiān)視的是任意 對象  而同步方法監(jiān)視的是 this 對象 有2種方法   1.同步塊  2.同步方法

7、     1. synchronized( Object)          .       2.方法前面加上 synchronized修飾    class  Test  public  static void main(String args)       SellThread st=new SellThread(); /創(chuàng)建一個(gè)實(shí)現(xiàn)

8、了implements接口的對象    new Thread(st).start();   /啟動(dòng)四個(gè)Thread     new Thread(st).start();    new Thread(st).start();    new Thread(st).start();     class SellThread   implements Runnable   int index

9、=100; */同步塊         /*public void run()               while(true)                   synchronized(this)   /通過對象監(jiān)視器枷鎖每次只有一

10、個(gè)線程進(jìn)入臨界區(qū)這個(gè)監(jiān)視對象可以是任意                   if(index>0)                    System.out.println(Thread.currentThread().getName()+"

11、sell tickets "+ index-);                             /同步塊       public void run()             while(true) 

12、;                     sell();  /調(diào)用同步方法進(jìn)行控制            /同步方法  synchronized public void sell()              

13、0;  if(index>0)                    System.out.println(Thread.currentThread().getName()+"sell tickets "+ index-);                /同步方

14、法 */ /同步快與同步方法實(shí)現(xiàn)同步  class  Test  public  static void main(String args)   throws  Exception         SellThread st=new SellThread(); /創(chuàng)建一個(gè)實(shí)現(xiàn)了implements接口的對象    new Thread(st).start();   /啟動(dòng)四個(gè)Thread  

15、60;  Thread.sleep(1);    st.b=true ;  /    new Thread(st).start();    class SellThread   implements Runnable   int index =100;    boolean b=false;  /用來實(shí)現(xiàn)同步    Object o=new Object(); /獲得一個(gè)檢測對象/同步塊 

16、0;       public void run()              if(b=false)              while(true)                 &#

17、160;       synchronized(o)  /同步塊可以監(jiān)視任意對象但是   同步方法只能監(jiān)視this 對象                                  &

18、#160;         if(index>0)                  System.out.println("this  "+Thread.currentThread().getName()+"sell ticket "+index-);     

19、                                else        while(true)           if(index>0)      &

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論