/* It implements a simple server class */ /* It implements a simple client class */ /* It implements main program */ /* The executable program has to be started java Example1 n */ /* where n is a number of rendez-vous repetitions */ class Client extends Thread { //thread private Server serv; private int n; Client (Server par_serv, int par_n) { serv = par_serv; n = par_n; } public void run () { for (int i = 0; i< n; i++) { try { sleep((100*i)%17); } catch (InterruptedException e) {} System.out.println("client calls r-v"); serv.entry_1(); System.out.println("client releases r-v"); } } } class Server extends Thread { //thread private Entry e1; Server () { e1 = new Entry();} synchronized void entry_1() { e1.call_entry(); System.out.println("r-v in progress"); e1.release_entry(); } public void run () { int i = 0; while (true) { try { sleep((100*i)%23); // sleeping } catch (InterruptedException e) {} System.out.println("server accepts r-v"); System.out.println("count =" + e1.get_count()); //kontrola e1.accept_entry(); System.out.println("count =" + e1.get_count()); //kontrola System.out.println("server releases r-v"); i++; } } } public class Example1 { public static void main (String[] argv) { int n = 0; if (argv.length == 1) { //checking number of arguments try { //trying convert string to int n = Integer.valueOf(argv[0]).intValue(); } catch (NumberFormatException e) { System.out.println("Invalid number format!"); } if (n > 0) { Server serv = new Server(); serv.setDaemon(true); Client cl = new Client(serv, n); serv.start(); cl.start(); try { cl.join(); } catch (InterruptedException e) {} } else { System.out.println("Number must be greater than 0!"); } } else { System.out.println("Invalid argument count!"); } } }