/* An example to demonstrate rendez-vous mechanismus implemented in Java. It implements a ClientServer that receives a parameter from another ClientServer object, increments it and passes it to another ClientServer object. This classe can run in normal mode, or in main mode. It runs in main mode when its the thread that first receives the parameter from the main programs, and initiates the cycle passing it to the next thread. It implements main program that shoul be started : java Program nThreads nIterations Parameter where: nThreads is the number of ClientServer threads nIterations is the number of times that the cicle will be run Parameter is the parameter to be passed to the main Thread */ class ClientServer extends Thread { //thread ClientServer pred, suc; private Entry e; int parameter, n; boolean main = false; //Normal constructor ClientServer(int n) { e = new Entry(); this.n = n; } //Constructor for main thread ClientServer(int n, int parameter) { e = new Entry(); this.n = n; main = true; this.parameter=parameter; } void setSuccessor(ClientServer suc) { this.suc = suc; } //entry procedure synchronized void entry_1(int n) { e.call_entry(); parameter = n; e.release_entry(); } public void run() { //for main thread if (main) { int originalParameter = parameter; for (int i = 0; i < n; i++) { suc.entry_1(originalParameter); e.accept_entry(); System.out.println("Iteration " + i + " :Main thread received parameter " + parameter); } } //for other threads else { for (int i = 0; i < n; i++) { e.accept_entry(); System.out.println("Iteration " + i +": "+ this.getName() + " received parameter " + parameter); parameter++; suc.entry_1(parameter); } } } } public class Example2 { public static void main(String[] argv) { int nIterations = 0, param = 0, nThreads = 0; if (argv.length == 3) { //checking number of arguments try { //trying convert string to int nThreads = Integer.valueOf(argv[0]).intValue(); nIterations = Integer.valueOf(argv[1]).intValue(); param = Integer.valueOf(argv[2]).intValue(); } catch (NumberFormatException e) { System.out.println("Invalid number format!"); } if (nIterations > 0 && nThreads > 0) { ClientServer[] array = new ClientServer[nThreads]; //Create the objects array[0] = new ClientServer(nIterations, param); for (int i = 1; i < nThreads; i++) { array[i] = new ClientServer(nIterations); } //Set the successors array[nThreads-1].setSuccessor(array[0]); for (int i = 0; i < nThreads - 1; i++) { array[i].setSuccessor(array[i + 1]); } //Initialize the threads for (int i = 0; i