/********************************************************************* *
* A Waiter relays an order Object to a Producer, waits in an * independent thread during the production, and then delivers the * result using a Consumer callback method. *
* A Waiter is useful for when a Consumer must place an order with a * Producer in an independent thread but does not wish to pass a direct * reference to itself to the Producer for the callback operation. * This primarily has applications in dealing with untrusted code such * as mobile agents. *
* References *
*
* Design Patterns: Elements of Reusable Object-Oriented Software
*
* 1995; Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides
*
* @author * David W. Croft * @version * 1998-04-07 *********************************************************************/ public class Waiter implements Runnable { ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// protected Producer producer; protected Consumer consumer; protected Object order; ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// public static void relay ( Producer producer, Consumer consumer, Object order ) ////////////////////////////////////////////////////////////////////// { new Thread ( new Waiter ( producer, consumer, order ) ).start ( ); } public Waiter ( Producer producer, Consumer consumer, Object order ) ////////////////////////////////////////////////////////////////////// { this.producer = producer; this.consumer = consumer; this.order = order; } public void run ( ) ////////////////////////////////////////////////////////////////////// { consumer.consume ( producer.produce ( order ) ); } ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// }