多執行緒-生產者消費者之等待喚醒機制

ZHOU_VIP發表於2017-06-03


package cn.itcast_05;

public class GetThread implements Runnable {
	
	private Student s;

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

	@Override
	public void run() {
		while (true) {
			synchronized (s) {
				if(!s.flag){
					try {
						s.wait(); //t2就等待了。立即釋放鎖。將來醒過來的時候,是從這裡醒過來的。
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				
				System.out.println(s.name + "---" + s.age);
				//林青霞---27
				//劉意---30
				
				//修改標記
				s.flag = false;
				//喚醒執行緒
				s.notify(); //喚醒t1
			}
		}
	}
}


package cn.itcast_05;

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) {
			synchronized (s) {
				//判斷有沒有
				if(s.flag){
					try {
						s.wait(); //t1等著,釋放鎖
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				
				if (x % 2 == 0) {
					s.name = "林青霞";
					s.age = 27;
				} else {
					s.name = "劉意";
					s.age = 30;
				}
				x++; //x=1
				
				//修改標記
				s.flag = true;
				//喚醒執行緒
				s.notify(); //喚醒t2,喚醒並不表示你立馬可以執行,必須還得搶CPU的執行權。
			}
			//t1有,或者t2有
		}
	}
}


package cn.itcast_05;

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


package cn.itcast_05;

/*
 * 分析:
 * 		資源類: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類中。
 */
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();
	}
}


相關文章