Consumer

Páginas: 2 (282 palabras) Publicado: 15 de octubre de 2012
import java.util.*;
import java.io.*;

/** Producer-consumer in Java, Take II.
*/
public class ProdCons2 {

/** Throughout the code, this is the object we synchronize on so this
* isalso the object we wait() and notifyAll() on.
*/
protected LinkedList list = new LinkedList();
protected int MAX = 10;
protected boolean done = false; // Also protected by lock on list./** Inner class representing the Producer side */
class Producer extends Thread {

public void run() {
while (true) {
Object justProduced = getRequestFromNetwork();// Get request from the network - outside the synch section.
// We're simulating this actually reading from a client, and it
// might have to wait for hours if the client is havingcoffee.
synchronized(list) {
while (list.size() == MAX) // queue "full"
try {
System.out.println("Producer WAITING");
list.wait(); //Limit the size
} catch (InterruptedException ex) {
System.out.println("Producer INTERRUPTED");
}
list.addFirst(justProduced);list.notifyAll(); // must own the lock
System.out.println("Produced 1; List size now " + list.size());
if (done)
break;
// yield(); // Useful for green threads &demo programs.
}
}
}

Object getRequestFromNetwork() { // Simulation of reading from client
// try {
// Thread.sleep(10); // simulate time passing duringread
// } catch (InterruptedException ex) {
// System.out.println("Producer Read INTERRUPTED");
// }
return(new Object());
}
}

/** Inner class representing theConsumer side */
class Consumer extends Thread {
public void run() {
while (true) {
Object obj = null;
synchronized(list) {
while (list.size() == 0) {...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Consumo Y Consumismo
  • El consumo me consume
  • El consumo me consume
  • El Consumo Me Consume
  • el consumo me consume
  • El consumo Me consume
  • Sociedad De Consumo: ¿Consumimos O Nos Consumen?
  • Consumo y consumismo

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS