wait()
/notify()
(2)Revised: May/2nd/2002; Since: Apr./28th/2002
$B$3$3$G$O%7%J%j%*$K4p$E$$$F(B $B$3$N $B6&M-%*%V%8%'%/%H!#(B $B6&M-%*%V%8%'%/%H$+$iCM$r $B6&M-%*%V%8%'%/%H$KCM$r%;%C%H$9$k%9%l%C%I!#0l$D$N(B $B$3$N%W%m%0%i%`$G$O(B $B$3$N%7%J%j%*$G:GA1$NJ}K!$O!"6&M-%*%V%8%'%/%HB&$G<+J,$rMxMQ$9$k%*%V%8%'%/%H$N $B $B6&M-%*%V%8%'%/%H(B $B0J>e$N%/%i%9$r%$%s%9%?%s%92=$7$FMxMQ$9$k$?$a$N%F%9%H%/%i%9$G$9!#(Bwait()
/notify()
$B$r$B%7%J%j%*(B
$B5!G=35MW(B
Warehouse
$B7?$N6&M-%*%V%8%'%/%H$r(B Consumer
$B%9%l%C%I$H(B Producer
$B%9%l%C%I$,MxMQ$7$^$9!#(BProducer
$B%9%l%C%I$,CM$r%;%C%H$9$k$N$rBT$C$F(B Consumer
$B%9%l%C%I$,CM$rWarehouse
put()
msg
$B$KCM$r%;%C%H$7!"(B state
$B$K(B true
$B$r%;%C%H!#(Bget()
msg
$B$NCM$rJV$7!"(B state
$B$K(B false
$B$r%;%C%H!#(BConsumer
Consumer
$B7?%*%V%8%'%/%H$,B8:_$9$k$+$b$7$l$J$$!#(B
run()
Warehouse
$B%*%V%8%'%/%H$N(B get()
$B$r8F$S=P$9!#(BProducer
Warehouse
$B7?6&M-%*%V%8%'%/%H$KBP$7$F!"J#?t$N(B Producer
$B7?%*%V%8%'%/%H$,B8:_$9$k$+$b$7$l$J$$!#(B
run()
Warehouse
$B%*%V%8%'%/%H$N(B put()
$B$r8F$S=P$9!#(B$BF14|(B
Consumer
$B%*%V%8%'%/%H$H(B Producer
$B%*%V%8%'%/%H$,6&M-%*%V%8%'%/%H$K=jDj$N=gHV$GGSB>E*$K%"%/%;%9$9$kI,MW$,$"$j$^$9$N$G!"6&M-%*%V%8%'%/%H$KBP$9$kF14|$,I,MW$G$9!#(Bnotify()
/notifyAll()
$B%a%=%C%I$rMxMQ$7$^$9!#$3$l$i$N%a%=%C%I$O(B Object
$B%/%i%9$GDj5A$5$l$F$*$j!"G$0U$N%/%i%9$G;H$&$3$H$,=PMh$^$9$,!"(B synchronized
$B$GF14|;XDj$5$l$?%3!<%I$NFbIt$G$J$1$l$P;H$($^$;$s!#(B$B%5%s%W%k(B
$B6&M-%*%V%8%'%/%H!'(B
Warehouse
Warehouse
$B$N%a%s%P!<$O
int msg
boolean state
msg
$B$,FI$_9~$^$l$F?7$7$$CM$,=q$-9~$_2DG=$K$J$l$P(B false
$B$K$J$j!"?7$7$$CM$,%;%C%H$5$l$FFI$_9~$_2DG=$K$J$l$P(B true
$B$K$J$j$^$9!#(Bvoid put(int val)
state
$B$,(B true
$B$G$"$l$P%m%C%/$rmsg
$B$K%;%C%H$7$^$9!#$3$N$H$-(B state
$B$O(B true
$B$KLa$7$^$9!#(Bint get()
state
$B$,(B false
$B$G$"$l$P%m%C%/$rmsg
$B$NCM$rJV$7$^$9!#$3$N$H$-(B state
$B$O(B false
$B$KLa$7$^$9!#(B
class Warehouse {
private int msg;
private boolean state = false;
public synchronized void put(int val) {
while (state == true) {
// $BCM$,%;%C%H$5$l$F$$$l$P7+$jJV$7(B
// $BFI$_9~$^$l$l$P7+$jJV$7=*N;(B
try {
wait(); // msg $B$KCM$,%;%C%H$5$l$F$$$l$PBT5!(B
} catch (InterruptedException e) {
System.out.println(e);
}
}
msg = val;
state = true; // msg $B$KCM$,%;%C%H$5$l$?$i(B true
notifyAll();
}
public synchronized int get() {
while (state == false) {
// $BFI$_9~$_:Q$_$J$i$P7+$jJV$7(B
// $B?7$7$$CM$,%;%C%H$5$l$l$P7+$jJV$7=*N;(B
try {
wait(); // msg $B$NCM$rFI$_9~$_:Q$_$J$i$PBT5!(B
} catch (InterruptedException e) {
System.out.println(e);
}
}
state = false; // msg $B$NCM$rFI$_9~$s$@$i(B false
notifyAll();
return msg;
}
}
Producer
Warehouse
$B7?%*%V%8%'%/%H$KCM$r%;%C%H$9$k%9%l%C%I!&%/%i%9$G$9!#(B
class Producer implements Runnable {
// $B6&M-%*%V%8%'%/%H(B
private Warehouse msg;
// $B%3%s%9%H%i%/%?(B
Producer(Warehouse obj) {
msg = obj;
}
// $B%9%l%C%I$N(B run() $B%a%=%C%I(B
public void run() {
// $B6&M-%*%V%8%'%/%H$KCM$r#52s%;%C%H(B
for (int i = 0; i < 5; i++) {
msg.put(i);
System.out.println("Producer: " + i);
// $B:n0YE*$K%?%$%_%s%0$r$:$i$9%9%j!<%W(B
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
Consumer
Warehouse
$B7?%*%V%8%'%/%H$NCM$r%2%C%H$9$k%9%l%C%I!&%/%i%9$G$9!#(B
class Consumer implements Runnable {
// $B6&M-%*%V%8%'%/%H(B
private Warehouse msg;
// $B%3%s%9%H%i%/%?(B
Consumer(Warehouse obj) {
msg = obj;
}
// $B%9%l%C%I$N(B run() $B%a%=%C%I(B
public void run() {
// $B6&M-%*%V%8%'%/%H$NCM$r#52s%2%C%H(B
for (int i = 0; i < 5; i++) {
int val = msg.get();
System.out.println("Consumer: " + val);
}
}
}
$B%3%s%H%m!<%k%/%i%9(B
ConsProdTest.java
:
class ConsProdTest {
public static void main(String[] args) {
// $B6&M-%*%V%8%'%/%H(B
Warehouse share = new Warehouse();
// $BFI$_
$B
C:\java\thread>javac ConsProdTest.java
C:\java\thread>java ConsProdTest
Consumer: 0
Producer: 0
Consumer: 1
Producer: 1
Consumer: 2
Producer: 2
Producer: 3
Consumer: 3
Consumer: 4
Producer: 4
C:\java\thread>
Copyright © 2002 SUGAI, Manabu. All Rights Reserved.
SEO
[PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送