Java Reference
In-Depth Information
}
list
.
addFirst
(
justProduced
);
list
.
notifyAll
();
// must own the lock
System
.
out
.
println
(
"Produced 1; List size now "
+
list
.
size
());
iif
(
done
)
break
break
;
// yield(); // Useful for green threads & demo programs.
}
}
}
Object
getRequestFromNetwork
() {
// Simulation of reading from client
// try {
// Thread.sleep(10); // simulate time passing during read
// } catch (InterruptedException ex) {
// System.out.println("Producer Read INTERRUPTED");
// }
return
return
(
new
new
Object
());
}
}
/** Inner class representing the Consumer side */
class
class
Consumer
Consumer
extends
extends
Thread
{
public
public
void
void
run
() {
while
while
(
true
true
) {
Object obj
=
null
null
;
synchronized
synchronized
(
list
) {
while
while
(
list
.
size
() ==
0
) {
try
try
{
System
.
out
.
println
(
"CONSUMER WAITING"
);
list
.
wait
();
// must own the lock
}
catch
catch
(
InterruptedException ex
) {
System
.
out
.
println
(
"CONSUMER INTERRUPTED"
);
}
}
obj
=
list
.
removeLast
();
list
.
notifyAll
();
int
int
len
=
list
.
size
();
System
.
out
.
println
(
"List size now "
+
len
);
iif
(
done
)
break
break
;
}
process
(
obj
);
// Outside synch section (could take time)
//yield(); DITTO
}
}