Stuck JCSP Recording

I have a very simple example of JCSP (sequential Java processes) code in which I try to write an integer to the One2OneInt channel and then read it.

package jcsp; import org.jcsp.lang.*; public class JCSP { public static void main(String[] args) { One2OneChannelInt chan = Channel.one2oneInt(); chan.out().write(5); System.out.println("Written..."); System.out.println(chan.in().read()); } } 

It seems that the value is never written to the channel, and the program just continues to work. "Written ..." is never printed.

+8
java channel
source share
2 answers

So, I found out about BlockingQueue and its implementation of SynchronousQueue . As stated here , SynchronousQueue works in the same way that CSP Channels works. This helped me understand what was wrong with my code. Simply put, you cannot write and read from a pipe in the same process. Channel - a way of processes for communication.

Similar to SynchronousQueue's put() , which will wait for another take() process to be called, CSP Channel's write() , which will wait for the corresponding read() called. The difference is that CSP Channels have ChannelOutput and ChannelInput objects through which objects are written and are red. Conversely, you can call put and take directly on the SynchronousQueue instance. Personally, I find SynchronousQueue much easier to understand, which probably refers to JCSP , which is not very popular.

However, if you are interested in how I made the above code in JCSP, here it is:

 public static class Process1 implements CSProcess { private ChannelOutputInt output; public Process1(ChannelOutputInt out) { output = out; } @Override public void run() { for (int i = 0; i < 1; i++) { System.out.println("Written..."); output.write(5); } output.write(-1); } } public static class Process2 implements CSProcess { private ChannelInputInt input; public Process2(ChannelInputInt in) { input = in; } @Override public void run() { int x = 0; while ((x = input.read()) > 0) { System.out.println(x); } } } public static void main(String[] args) { One2OneChannelInt chan = Channel.one2oneInt(); Process1 process1 = new Process1(chan.out()); Process2 process2 = new Process2(chan.in()); Parallel parallel = new Parallel(); parallel.addProcess(process1); parallel.addProcess(process2); parallel.run(); } 
+6
source share

The problem is that the pipe is not buffered, so your call to write() will block until another pipe starts reading from the pipe. As soon as another process calls read() , "Written ..." will be printed.

A BlockingQueue with a capacity of 0 behaves similarly to a JCSP channel

0
source share

All Articles