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(); }
Schizo
source share