public class TortoiseHareRace { public static void main(String[] args) { int totalStep = 10; int tortoiseStep = 0; int hareStep = 0; boolean[] flags = {true,false}; System.out.println("龜兔賽跑開始了..."); while(tortoiseStep < totalStep && hareStep < totalStep){ tortoiseStep++; System.out.println("烏龜跑了"+tortoiseStep+"步..."); boolean isHareSleep = flags[((int)(Math.random()*10))%2]; if(isHareSleep){ System.out.println("兔子睡着了zzzz"); }else{ hareStep += 2; System.out.println("兔子跑了"+hareStep+"步..."); } } } }
public class TortoiseHareRace { public static void main(String[] args) { Tortoise tortoise = new Tortoise(10); Hare hare = new Hare(10); Thread tortoiseThread = new Thread(tortoise); Thread hareThread = new Thread(hare); tortoiseThread.start(); hareThread.start(); } }
Tortoise線程類:java
package a; public class Tortoise implements Runnable { private int step; private int totalstep=10; public Tortoise(int step){ this.step=step; } public void setStep(int step){ this.step=step; } public int getStep(){ return step; } public void run(){ for (step=0;step<=10;step++) { synchronized(this){ if (step < this.totalstep) { try { Thread.sleep(300); step++; System.out.println("烏龜走了" + step + "步"); } catch (InterruptedException e) { e.printStackTrace(); } } } } } public String toString(){ return "烏龜步數:"+this.step; } }
Hare線程類:多線程
package a; public class Hare implements Runnable { private int step; private int harestep; public Hare(int step){ this.step=step; } public void setStep(int step){ this.step=step; } public int getStep(){ return step; } public String toString(){ return "兔子步數:"+this.step; } public void run() { boolean[] flags = { true, false }; for(step=0;step<=10;step++) { synchronized(this){ while (harestep < step) { boolean isHareSleep = flags[((int) (Math.random() * 10)) % 2]; if (isHareSleep) { System.out.println("兔子睡着了zzzz"); } else { harestep += 2; System.out.println("兔子跑了" + harestep + "步..."); } } } } } }
class Consumer implements Runnable { private Clerk clerk; public Consumer(Clerk clerk) { this.clerk = clerk; } public void run() { System.out.println("消費者開始消耗整數......"); // 消耗10個整數 for(int i = 1; i <= 10; i++) { try { // 等待隨機時間 Thread.sleep((int) (Math.random() * 3000)); } catch(InterruptedException e) { e.printStackTrace(); } clerk.getProduct();// 從店員處取走整數 } } } class Producer implements Runnable { private Clerk clerk; public Producer(Clerk clerk) { this.clerk = clerk; } public void run() { System.out.println( "生產者開始生產整數......"); // 生產1到10的整數 for(int product = 1; product <= 10; product++) { try { Thread.sleep((int) Math.random() * 3000); } catch(InterruptedException e) { e.printStackTrace(); } clerk.setProduct(product); // 將產品交給店員 } } } public class ProductTest { public static void main(String[] args) { Clerk clerk = new Clerk(); Thread consumerThread = new Thread(new Consumer(clerk)); Thread producerThread = new Thread(new Producer(clerk)); consumerThread.start(); producerThread.start(); } } class Clerk { private int product = -1; // -1 表示目前沒有產品 // 這個方法由生產者呼叫 public void setProduct(int product) { this.product = product; System.out.printf("生產者設定 (%d)%n", this.product); } // 這個方法由消費者呼叫 public int getProduct() { int p = this.product; System.out.printf("消費者取走 (%d)%n", this.product); return p; } }
沒有進行代碼同步和喚醒。
運行結果爲:dom
修改後的爲:學習
class Clerk { private int product = -1; // -1 表示目前沒有產品 private int p ; // 這個方法由生產者呼叫 public synchronized void setProduct(int product) { if (this.product != -1) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.product = product; p = this.product; System.out.printf("生產者設定 (%d)%n", this.product); getProduct(); try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } this.product = -1; super.notify(); } // 這個方法由消費者呼叫 public synchronized int getProduct() { if (this.product == -1) { try { wait(); }catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } System.out.printf("消費者取走 (%d)%n", p); this.product = -1; super.notify(); return this.product; } }
運行結果爲:
生產者開始生產整數......
消費者開始消耗整數......
生產者設定 (1)
消費者取走 (1)
生產者設定 (2)
消費者取走 (2)
生產者設定 (3)
消費者取走 (3)
生產者設定 (4)
消費者取走 (4)
生產者設定 (5)
消費者取走 (5)
生產者設定 (6)
消費者取走 (6)
消費者取走 (6)
生產者設定 (7)
消費者取走 (7)
生產者設定 (8)
消費者取走 (8)
生產者設定 (9)
消費者取走 (9)
生產者設定 (10)
消費者取走 (10)this
4.其餘須要總結的內容。
最後一次實驗做業,慢慢的也知道到了學習的方法和總結的方法,也對java有了必定的瞭解。做業結束,學習繼續。線程
完成實驗內容,代碼上傳到碼雲,對完成實驗內容過程當中遇到的問題、解決方案和思考等進行概括總結,注意代碼中必須有必要的註釋。
格式以下:
(一)程序設計思路:實現Teacher和Task兩個類的Runnable接口。80份做業,建立一個對象,共享一個目標對象。
(二)程序設計思路:用Bank類和User類並把用戶類實現Runnable類接口。設計