waiter.java

来自「A Waiter relays an order Object to a Pro」· Java 代码 · 共 62 行

JAVA
62
字号
     /*********************************************************************
     * <P>
     * 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.
     * <P>
     * 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.
     * <P>
     * <B>References</B>
     * <P>
     * <A HREF="http://www.amazon.com/exec/obidos/ISBN=0201633612/">
     * Design Patterns:  Elements of Reusable Object-Oriented Software</A>
     * <BR>
     * 1995; Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides
     * <P>
     * @author
     *   <A HREF="http://www.alumni.caltech.edu/~croft">David W. Croft</A>
     * @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 ) );
     }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////
     }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?