Java Reference
In-Depth Information
Producer ( BlockingQueue < Object > theQueue ) { this
this . queue = theQueue ; }
public
public void
void run () {
try
try {
while
while ( true
true ) {
Object justProduced = getRequestFromNetwork ();
queue . put ( justProduced );
System . out . println (
"Produced 1 object; List size now " + queue . size ());
iif ( done ) {
return
return ;
}
}
} catch
catch ( InterruptedException ex ) {
System . out . println ( "Producer INTERRUPTED" );
}
}
Object getRequestFromNetwork () {
// Simulation of reading from client
try
try {
Thread . sleep ( 10 ); // simulate time passing during read
} catch
catch ( InterruptedException ex ) {
System . out . println ( "Producer Read INTERRUPTED" );
}
return
return new
new Object ();
}
}
/** Inner class representing the Consumer side */
class
class Consumer
Consumer implements
implements Runnable {
protected
protected BlockingQueue < Object > queue ;
Consumer ( BlockingQueue < Object > theQueue ) { this
this . queue = theQueue ; }
public
public void
void run () {
try
try {
while
while ( true
true ) {
Object obj = queue . take ();
int
int len = queue . size ();
System . out . println ( "List size now " + len );
process ( obj );
iif ( done ) {
return
return ;
}
}
} catch
catch ( InterruptedException ex ) {
Search WWH ::




Custom Search