java執行緒間通訊 等待喚醒機制

2021-08-10 19:24:36 字數 3417 閱讀 8550

等待喚醒機制:

涉及方法: 

1、wait():讓執行緒處於凍結狀態,被wait的執行緒會儲存到執行緒池中

2、notify():喚醒執行緒池中任一線程(隨機)

3、nitifyall():喚醒執行緒池中所有執行緒

這些方法必須定義在同步中,因為這些方法是用於操作執行緒狀態的方法,必須要明確操作的是哪個鎖上的執行緒

這些方法都定義在object類中,因為這些方法都是監視器的方法,監視器其實就是鎖,鎖可以是任意的物件,任意的物件呼叫的方式一定定義在object類中

等待喚醒機制的示例:

未用等待喚醒:

//資源

class resource

//輸入

class input implements runnable

public void run()

else

}x = (x+1)%2;

} }}//輸出

class output implements runnable

public void run()

} }}

class resourcedemo

}

class resource

catch(interruptedexception e){}

this.name = name;

this.*** = ***;

flag = true;

this.notify();

} public synchronized void out()

catch(interruptedexception e){}

system.out.println(name+"...+...."+***);

flag = false;

notify(); }}

//輸入

class input implements runnable

public void run()

else

x = (x+1)%2;

} }}//輸出

class output implements runnable

public void run() }

}class resourcedemo3

}

此為

單生產者單消費者示例。

class resource

catch(interruptedexception e){}// t1 t0

this.name = name + count;//

count++;//2 3 4

system.out.println(thread.currentthread().getname()+"...生產者..."+this.name);

flag = true;

notifyall();

} public synchronized void out()// t3

catch(interruptedexception e){} //t2 t3

system.out.println(thread.currentthread().getname()+"...消費者........"+this.name);

flag = false;

notifyall(); }}

class producer implements runnable

public void run() }

}class consumer implements runnable

public void run() }

}class producerconsumerdemo

}

注意:這裡的while(flag)不能用if,if判斷標記只有一次,會導致不該執行的執行緒執行,出現資料錯誤的情況。

while解決了執行緒獲取執行權後是否要執行。

notify:只喚醒了乙個執行緒,如果本方喚醒了本方,沒有意義。而且while判斷標記+notify會導致死鎖。

notifyall:解決了本方執行緒一定會喚醒對方執行緒的問題

不過這樣會降低效率,jdk1.5版本將同步和鎖封裝成了物件,用lock介面代替了同步**塊和同步函式,更加靈活,可以乙個鎖加多個監視器

lock():獲取鎖。

unlock():釋放鎖,通常需要定義finally**塊中。

condition介面:出現替代了object中的wait notify notifyall方法。

將這些監視器方法單獨進行了封裝,變成condition監視器物件。

可以任意鎖進行組合。

await();    ————(wait())

signal();   ————(notify())

signalall();————(notifyall())

這樣上面**就可以優化:

class resource

catch(interruptedexception e){}// t1 t0

trycatch(interruptedexception e){}// t1 t0

this.name = name + count;//烤鴨1 烤鴨2 烤鴨3

count++;//2 3 4

system.out.println(thread.currentthread().getname()+"...生產者5.0..."+this.name);

flag = true;

// notifyall();

// con.signalall();

consumer_con.signal();

} finally

} public void out()// t2 t3

catch(interruptedexception e){} //t2 t3

trycatch(interruptedexception e){} //t2 t3

system.out.println(thread.currentthread().getname()+"...消費者.5.0......."+this.name);

flag = false;

// notifyall();

// con.signalall();

producer_con.signal();

} finally

}}class producer implements runnable

public void run() }

}class consumer implements runnable

public void run() }

}class producerconsumerdemo2

}

Java 執行緒間通訊 等待喚醒機制

分析 我們期望的結果是,當輸入執行緒一次 input 操作資源 res 後,我們希望輸入執行緒 input 失去執行權,讓輸出執行緒 output 獲取cpu執行權,當輸出執行緒 output 一次列印完成後,我們希望輸出執行緒 output 失去執行權,這時候讓輸入執行緒 input 獲取執行權,...

執行緒間通訊 等待喚醒機制

執行緒間通訊 其實就是多個執行緒在操作同乙個資源,但是操作的動作不同 class res2 class input2 implements runnable public void run catch interruptedexception e if x 0 else x x 1 2 r.flag...

java基礎12 執行緒間通訊 等待喚醒機制

今天學習了等待喚醒機制,通過乙個例子做個小小的總結。需求是對某個資料進行修改和輸出操作,要求兩個操作同時進行,且先修改後輸出。設計思路 1,需要乙個公共資料類。兩個執行緒,乙個input 乙個output。2,input和output應該是兩個執行緒,這樣才能同時進行。3,input和output的...