Java作業第八章

來源:互聯網
上載者:User

        示範線程不考慮完全的邏輯問題。例如50=20+20+5+5或50=10+10+20+5等其他情況。。。本例子在判斷50找零的時候,只判斷了這兩種情況,其他的就沒考慮的。重在示範線程的過程。。。。

Code:
  1. package cc.nothing2012.blogServlet;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.List;   
  5.   
  6. /*=======題目:模仿例8-10,設計5個人排隊買票,並規定賣票規則和排隊順序==========*/  
  7.   
  8. /*  
  9.  * 類比五個人排隊買票,每人買1張票。售票員有1張20元和1張5元的錢,電影票一張5元。  
  10.  * 排隊從頭到尾的順序是:關五十、林二十、餘五、李十、楊五 
  11.  * 最後的買票順序是:餘五、李十、林二十、關五十、楊五  
  12.  */  
  13. public class ch8_4 {   
  14.     public static List<String> theResult = new ArrayList<String>(); // 存放最後買票先後的結果   
  15.   
  16.     public static void main(String[] args) {   
  17.         String[] sName = { "關五十", "林二十", "餘五", "李十", "楊五" };   
  18.         Cinema c = new Cinema(sName);   
  19.         Thread[] theads = { new Thread(c), new Thread(c), new Thread(c),   
  20.                 new Thread(c), new Thread(c) };   
  21.         // for(int i=0;i<5;i++){ //初始化5個進程   
  22.         // theads[i]=new Thread(c);   
  23.         // }   
  24.         for (int i = 0; i < 5; i++) {   
  25.             theads[i].setName(sName[i]);   
  26.         }   
  27.         for (int i = 0; i < 5; i++) {   
  28.             theads[i].start();   
  29.         }   
  30.   
  31.         while (true) {// 將最後結果進行列印   
  32.             try {   
  33.                 Thread.sleep(1000);   
  34.             } catch (InterruptedException e) {   
  35.                 e.printStackTrace();   
  36.             }   
  37.             if (theResult != null) {   
  38.                 if (theResult.size() == 5) {   
  39.                     printMessage("最後買票先後的結果");   
  40.                     for (int i = 0; i < 5; i++) {   
  41.                         System.out.print(theResult.get(i) + "/t");   
  42.                     }   
  43.                     break;   
  44.                 }   
  45.             }   
  46.         }   
  47.     }   
  48.   
  49.     // 負責列印資訊。   
  50.     public static void printMessage(Object o) {   
  51.         System.out.println(o);   
  52.     }   
  53. }   
  54.   
  55. // =======================   
  56. class Cinema implements Runnable {   
  57.     TicketSeller seller;   
  58.     String[] sName;   
  59.   
  60.     public Cinema(String[] sName) {   
  61.         seller = new TicketSeller();   
  62.         this.sName = sName;   
  63.     }   
  64.   
  65.     public void run() {   
  66.         if (Thread.currentThread().getName().equals(sName[0])) {   
  67.             seller.sellTicket(50);   
  68.         } else if (Thread.currentThread().getName().equals(sName[1])) {   
  69.             seller.sellTicket(20);   
  70.         } else if (Thread.currentThread().getName().equals(sName[2])) {   
  71.             seller.sellTicket(5);   
  72.         } else if (Thread.currentThread().getName().equals(sName[3])) {   
  73.             seller.sellTicket(10);   
  74.         } else if (Thread.currentThread().getName().equals(sName[4])) {   
  75.             seller.sellTicket(5);   
  76.         }   
  77.     }   
  78.   
  79. }   
  80.   
  81. // ============售票員賣票的操作==============   
  82. class TicketSeller {   
  83.     int fiveNumber = 1, tenNumber = 0, twentyNumber = 1, fiftyNumber = 0; // 小票員現有5元和20元的錢各一張   
  84.   
  85.     public synchronized void sellTicket(int receiveMoney) {   
  86.         String sName = Thread.currentThread().getName();   
  87.         if (receiveMoney == 5) { // 票一張是5元,剛好5元就直接賣給他,不用找零了。   
  88.             fiveNumber += 1;   
  89.             ch8_4.printMessage(sName + "給小票員5元錢,小票員賣個" + sName + "一張票不用找贖");   
  90.             ch8_4.theResult.add(sName);   
  91.         } else if (receiveMoney == 10) {   
  92.             while (fiveNumber < 1) {   
  93.                 ch8_4.printMessage(sName + "給售票員10元");   
  94.                 ch8_4.printMessage("小票員請" + sName + "靠邊等一會");   
  95.                 try {   
  96.                     wait();   
  97.                 } catch (InterruptedException e) {   
  98.                     e.printStackTrace();   
  99.                 }   
  100.                 ch8_4.printMessage(sName + "結束等待,繼續買票");   
  101.             }   
  102.             // 有零錢了   
  103.             fiveNumber = fiveNumber - 1;   
  104.             tenNumber += 1;   
  105.             ch8_4.printMessage(sName + "給售票員10元錢,售票員賣給" + sName + "一張票,找贖5元");   
  106.             ch8_4.theResult.add(sName);   
  107.         } else if (receiveMoney == 20) {   
  108.             while (fiveNumber < 1 || tenNumber < 1) {   
  109.                 ch8_4.printMessage(sName + "給售票員20元/n售票員請" + sName + "靠邊等一會兒");   
  110.                 try {   
  111.                     wait();   
  112.                 } catch (InterruptedException e) {   
  113.                     e.printStackTrace();   
  114.                 }   
  115.                 ch8_4.printMessage(sName + "結束等待,繼續買票");   
  116.             }   
  117.             fiveNumber = fiveNumber - 1;   
  118.             tenNumber = tenNumber - 1;   
  119.             twentyNumber += 1;   
  120.             ch8_4.printMessage(sName + "給售票員20元錢,售票員賣給" + sName + "一張票,找零15元");   
  121.             ch8_4.theResult.add(sName);   
  122.         } else if (receiveMoney == 50) {   
  123.             String flag = "";   
  124.             // boolean isOK=true;   
  125.             while (true) {   
  126.                 if (fiveNumber > 0) { // 45塊的組合有兩種情況   
  127.                     if (twentyNumber == 2) {   
  128.                         flag = "2twenty";   
  129.                         break;   
  130.                         // isOK=false;   
  131.                     } else if (twentyNumber == 1 && tenNumber == 2) {   
  132.                         flag = "1twenty";   
  133.                         // isOK=false;   
  134.                         break;   
  135.                     }   
  136.                 }   
  137.                 ch8_4.printMessage(sName + "給售票員50元/n售票員請" + sName + "靠邊等一會兒");   
  138.                 try {   
  139.                     wait();   
  140.                 } catch (InterruptedException e) {   
  141.                     e.printStackTrace();   
  142.                 }   
  143.                 ch8_4.printMessage(sName + "結束等待,繼續買票");   
  144.             }   
  145.             if (flag.endsWith("2twenty")) {   
  146.                 fiveNumber -= 1;   
  147.                 twentyNumber = twentyNumber - 2;   
  148.                 fiftyNumber += 1;   
  149.             } else if (flag.equals("1twenty")) {   
  150.                 fiveNumber = fiveNumber - 1;   
  151.                 tenNumber = tenNumber - 2;   
  152.                 twentyNumber = twentyNumber - 1;   
  153.             }   
  154.             ch8_4.printMessage(sName + "給售票員50元錢,售票員賣給" + sName + "一張票,找零45元");   
  155.             ch8_4.theResult.add(sName);   
  156.         }   
  157.         notifyAll();   
  158.     }   
  159. }   

運行結果如下:

關五十給售票員50元
售票員請關五十靠邊等一會兒
林二十給售票員20元
售票員請林二十靠邊等一會兒
餘五給小票員5元錢,小票員賣個餘五一張票不用找贖
關五十結束等待,繼續買票
關五十給售票員50元
售票員請關五十靠邊等一會兒
林二十結束等待,繼續買票
林二十給售票員20元
售票員請林二十靠邊等一會兒
李十給售票員10元錢,售票員賣給李十一張票,找贖5元
關五十結束等待,繼續買票
關五十給售票員50元
售票員請關五十靠邊等一會兒
林二十結束等待,繼續買票
林二十給售票員20元錢,售票員賣給林二十一張票,找零15元
關五十結束等待,繼續買票
關五十給售票員50元
售票員請關五十靠邊等一會兒
楊五給小票員5元錢,小票員賣個楊五一張票不用找贖
關五十結束等待,繼續買票
關五十給售票員50元錢,售票員賣給關五十一張票,找零45元
最後最票先後的結果
餘五 李十 林二十 楊五 關五十 

相關文章

聯繫我們

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