
// CigSmokers.java
// Andrew Davison, ad@coe.psu.ac.th, May 2023

/* The Cigarette smokers problem:
  https://en.wikipedia.org/wiki/Cigarette_smokers_problem

  A cigarette requires three ingredients: tobacco, paper, and matches. 

  There are three smokers around a table, each with an infinite 
  supply of one ingredient — one smoker has an infinite supply 
  of tobacco, another has paper, and the third has matches.

  There is also an agent who allows the smokers to make 
  their cigarettes by arbitrarily 
  selecting two supplies to place on the table. 

  The smoker who has the third supply must remove the two 
  items from the table, and use them to make a cigarette. 

  Once the smoker has finished his cigarette, the agent places 
  two new random supplies on the table. 

  This process continues forever. 

  Usage:
    > compile.bat CigSmokers.java
    > run.bat CigSmokers

    Part of the application made up of:
       * CigSmokers.java
       * Smoker.java
       * Agent.java
       * Supplies.java
*/

import java.util.*;

import org.jspace.*;


public class CigSmokers
{
  public static void main(String[] args)
                             throws InterruptedException
  {
    Space space = new SequentialSpace();
    // Space space = new VisSpace(500);   // faster than the default
    PrintAreas pas = new PrintAreas(4);

    new Thread( new Agent(pas, 0, space),"Agent").start();

    /* the first argument is the smoker's own ingredient,
       and the other two arguments are what he
       needs in order to make a cigarette */
    new Thread( new Smoker(Supplies.TOBACCO, 
           Supplies.PAPER, Supplies.MATCHES, pas, 1, space),
           "Smoker with Tobacco").start();

    new Thread( new Smoker(Supplies.PAPER, 
           Supplies.TOBACCO, Supplies.MATCHES, pas, 2, space),
           "Smoker with Paper").start();

    new Thread( new Smoker(Supplies.MATCHES, 
           Supplies.TOBACCO, Supplies.PAPER, pas, 3, space),
           "Smoker with Matches").start();
  } /* main */

}  // end of CigSmokers class
