
// ProducerConsumer.java
// Michele Loreti and the jSpace Developers, 2017
// Modified by Andrew Davison, ad@coe.psu.ac.th, May 2023

/* Have two producer threads each send NUM_ITEMS items
   to a single consumer thread by communicating via a 
   single space.

  Original example at:
    https://github.com/pSpaces/jSpace/tree/master/examples/ProducerConsumer/src/main/java/org/jspace/examples/pc

  Usage:
    > compile.bat ProducerConsumer.java
    > run.bat ProducerConsumer

    Part of the application made up of:
       * ProducerConsumer.java
       * ProducerAgent.java
       * ConsumerAgent.java

  Other version: 
    * RemoteProducerConsumer.java
*/

import org.jspace.*;


public class ProducerConsumer
{
  private static final int NUM_ITEMS = 10;

  public static void main(String[] args) 
                            throws InterruptedException
  {
    // choose one of these spaces:
    // Space space = new SequentialSpace();
    Space space = new VisSpace(1000);
       // reduced animation delay from 2000 ms;

       /* the visualization usually runs out of space
          to display all the tuples, but this doesn't
          affec execution. Messages are printed to 
          stdout instead. 
       */

    Thread t1 = new Thread( 
         new ProducerAgent(0, "hammer", space, NUM_ITEMS),
                                          "Producer-0");
    Thread t2 = new Thread( 
         new ProducerAgent(1, "anvil", space, NUM_ITEMS), 
                                          "Producer-1");
    Thread t3 = new Thread( 
         new ConsumerAgent(space, NUM_ITEMS*2), "Consumer");
    t3.start();
    t1.start();
    t2.start();

    SpaceUtils.waitTerminated(t1, t2, t3);
  } /* main */

} // end of ProducerConsumer class
