import util.comm.*; import util.MoreOutputStream; /** * This class will compute in parallel (using threads) the result of * 2 * Ax - Bx + C * * And generate values suitable for plotting from 1 to 10. * First we decompose it into a postfix notation: * * ((x x *) A *) (B x *) - ) C + * * and decide how many threads we need: * a b op * +----------------------+ * res1 : X X * * res2 : B X * * res3 : res1 A * * res4 : res3 res2 - * res5 : res4 C + * * A total of 5 threads with the result coming out on result 5. * * A total of 5 + 4 data channels. * */ public class Quadratic { public static void main(String args[]) { DataChannel X = DataChannel.getChannel("X"); DataChannel X2 = DataChannel.getChannel("X2"); DataChannel A = DataChannel.getChannel("a"); DataChannel B = DataChannel.getChannel("b"); DataChannel C = DataChannel.getChannel("c"); DataChannel answer = DataChannel.getChannel("res5", Thread.currentThread(), 2); Computer res[] = new Computer[5]; System.out = new java.io.PrintStream(new MoreOutputStream()); res[0] = Computer.create("X", "X2", "res1", Computer.MUL); res[1] = Computer.create("X", "b", "res2", Computer.MUL); res[2] = Computer.create("res1", "a", "res3", Computer.MUL); res[3] = Computer.create("res3", "res2", "res4", Computer.SUB); res[4] = Computer.create("res4", "c", "res5", Computer.ADD); /* now start them up. */ for (int i = 0; i < res.length; i++) res[i].start(); /* now write in the values. */ for (int i = 0; i < 5; i++) { C.putValue(new Integer(10)); B.putValue(new Integer(5)); A.putValue(new Integer(3)); X.putValue(new Integer(i)); X2.putValue(new Integer(i)); int check = ((i * i) * 3) - (5 * i) + 10; try { System.out.println("X = "+i+", result = "+ ((Integer) answer.getValue())+ " ["+check+"]"); } catch (DataChannelException e) { System.out.println("Overrun on the answer!"); } } for (int i = 0; i < res.length; i++) res[i].stop(); try { Thread.currentThread().sleep(1000); } catch (InterruptedException e) { } } }