JAVA第十次做業

1、做業要求

(一)學習總結

1.用思惟導圖對java多線程的學習內容進行總結。

2.下面是一個單線程實現的龜兔賽跑遊戲。

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+"步...");
        }
    }       
}
}

閱讀程序,採用實現Runnable接口的方式用多線程實現這個小遊戲。下面給出主線程類,補充Tortoise線程類和Hare線程類。

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 + "步...");
                    }
                }
            }
         }
     }
}

3.下面的程序是模擬了生產者——消費者問題,生產者生產10個數,消費者依次消費10個數,運行程序,看結果是否正常?存在什麼問題?說明緣由。使用synchronized, wait, notify解決程序出現的問題。寫出修改的部分程序便可。

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有了必定的瞭解。做業結束,學習繼續。線程

(二)實驗總結

實驗內容:

1.模擬三個老師同時分發80分做業,每一個老師至關於一個線程。

2.模擬一個銀行存款的程序。假設有兩個儲戶都去銀行往同一個帳戶進行存款,一次存100,每人存三次。要求儲戶每存一次錢,帳戶餘額增長100,並在控制檯輸出當前帳戶的餘額。

完成實驗內容,代碼上傳到碼雲,對完成實驗內容過程當中遇到的問題、解決方案和思考等進行概括總結,注意代碼中必須有必要的註釋。
格式以下:
(一)程序設計思路:實現Teacher和Task兩個類的Runnable接口。80份做業,建立一個對象,共享一個目標對象。
(二)程序設計思路:用Bank類和User類並把用戶類實現Runnable類接口。設計

(三)代碼託管(務必連接到你的項目)

碼雲commit歷史截圖

上傳實驗項目代碼到碼雲,在碼雲項目中選擇「統計-commits」,設置搜索時間段,搜索本週提交歷史,並截圖。

相關文章
相關標籤/搜索