多執行緒-生產者消費者之等待喚醒機制程式碼優化

ZHOU_VIP發表於2017-06-03

package cn.itcast_07;

public class GetThread implements Runnable {
	
	private Student s;

	public GetThread(Student s) {
		this.s = s;
	}

	@Override
	public void run() {
		while (true) {
			s.get();
		}
	}
}


package cn.itcast_07;

public class SetThread implements Runnable {

	private Student s;
	private int x = 0;

	public SetThread(Student s) {
		this.s = s;
	}

	@Override
	public void run() {
		while (true) {
			if (x % 2 == 0) {
				s.set("林青霞", 27);
			} else {
				s.set("劉意", 30);
			}
			x++;
		}
	}
}


package cn.itcast_07;

public class Student {
	
	private String name;
	private int age;
	private boolean flag; // 預設情況是沒有資料,如果是true,說明有資料

	public synchronized void set(String name, int age) {
		// 如果有資料,就等待
		if (this.flag) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		// 設定資料
		this.name = name;
		this.age = age;

		// 修改標記
		this.flag = true;
		this.notify();
	}

	public synchronized void get() {
		// 如果沒有資料,就等待
		if (!this.flag) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		// 獲取資料
		System.out.println(this.name + "---" + this.age);

		// 修改標記
		this.flag = false;
		this.notify();
	}
}


package cn.itcast_07;

/*
 * 分析:
 * 		資源類:Student	
 * 		設定學生資料:SetThread(生產者)
 * 		獲取學生資料:GetThread(消費者)
 * 		測試類:StudentDemo
 * 
 * 問題1:按照思路寫程式碼,發現資料每次都是:null---0
 * 原因:我們在每個執行緒中都建立了新的資源,而我們要求的時候設定和獲取執行緒的資源應該是同一個
 * 如何實現呢?
 * 		在外界把這個資料建立出來,通過構造方法傳遞給其他的類。
 * 
 * 問題2:為了資料的效果好一些,我加入了迴圈和判斷,給出不同的值,這個時候產生了新的問題
 * 		A:同一個資料出現多次
 * 		B:姓名和年齡不匹配
 * 原因:
 * 		A:同一個資料出現多次
 * 			CPU的一點點時間片的執行權,就足夠你執行很多次。
 * 		B:姓名和年齡不匹配
 * 			執行緒執行的隨機性
 * 執行緒安全問題:
 * 		A:是否是多執行緒環境		是
 * 		B:是否有共享資料		是
 * 		C:是否有多條語句操作共享資料	是
 * 解決方案:
 * 		加鎖。
 * 		注意:
 * 			A:不同種類的執行緒都要加鎖。
 * 			B:不同種類的執行緒加的鎖必須是同一把。
 * 
 * 問題3:雖然資料安全了,但是呢,一次一大片不好看,我就想依次的一次一個輸出。
 * 如何實現呢?
 * 		通過Java提供的等待喚醒機制解決。
 * 
 * 等待喚醒:
 * 		Object類中提供了三個方法:
 * 			wait():等待
 * 			notify():喚醒單個執行緒
 * 			notifyAll():喚醒所有執行緒
 * 		為什麼這些方法不定義在Thread類中呢?
 * 			這些方法的呼叫必須通過鎖物件呼叫,而我們剛才使用的鎖物件是任意鎖物件。
 * 			所以,這些方法必須定義在Object類中。
 * 
 * 最終版程式碼中:
 * 		把Student的成員變數給私有的了。
 * 		把設定和獲取的操作給封裝成了功能,並加了同步。
 * 		設定或者獲取的執行緒裡面只需要呼叫方法即可。
 */
public class StudentDemo {
	public static void main(String[] args) {
		//建立資源
		Student s = new Student();
		
		//設定和獲取的類
		SetThread st = new SetThread(s);
		GetThread gt = new GetThread(s);

		//執行緒類
		Thread t1 = new Thread(st);
		Thread t2 = new Thread(gt);

		//啟動執行緒
		t1.start();
		t2.start();
	}
}



相關文章