Nice TWiki > Doc > CodeExamples > SimpleProducerConsumerCspExample (r1.3) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
/* Please try the NoviceExamples before these examples
To compile:
   >nicec --classpath "jcspclasses.jar;nice-csp.jar" --sourcepath .. -a prodcons.jar prodcons
To run:
   java -cp "jcspclasses.jar;prodcons.jar" prodcons.fun
*/

import nice.csp;

let int MAX = 5;

void main(String[] args){
   let chan = new One2OneChannelInt();

   let p = new ProdCon(channel: chan);
   let c = new ProdCon(channel: chan);

   par ([
      seq( ()=>{ c.consume(); } )
     ,seq( ()=>{ p.produce; } )                     
   ]).run;
}

class ProdCon {
   One2OneChannelInt channel;
   int count = 0;

   void produce(){
      while(count < MAX){
         count++;
         channel.write(count);
      }
   }

   void consume(){
      while(count < MAX){
         count = channel.read;
         println("Consumed " + count);
      }
   }
}

/* Notes - language
Compare with Java at the "Win32 Language Shootout"
Compare with Nice using Java concurrency in the "Language Shootout Examples"

Experimental use of the "Communicating Sequential Processes for Java TM (JCSP)" library.

   >java -cp "jcspclasses.jar;prodcons.jar" prodcons.fun
   Consumed 1
   Consumed 2
   Consumed 3
   Consumed 4
   Consumed 5
*/
-- IsaacGouy - 24 Feb 2004

Topic SimpleProducerConsumerCspExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.9 | > | r1.8 | > | r1.7 | More }
Revision r1.3 - 24 Feb 2004 - 19:49 GMT - IsaacGouy
Parents: WebHome > CodeExamples
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.