/* Threads in Java - basic example - wo threads and one */ /* monitor. */ /* Monitor implements a mailbox for an exchange of messages. */ /* The mailbox can store only one message. Every message is */ /* a string. One thread (producent) reads a string from the */ /* keyboard, the second thread (consumer) waits for a message,*/ /* reads it from the mailbox and write it on the screen. */ /* For the string: "zhebni potvoro" the threads finishes their*/ /* life and the program ends as well. */ /* The application is started: java Program */ /* What to modify: */ /* 1) To rebuild the program for more files, i.e. to make more*/ /* public classes. */ /* 2) The producer's thed thinks off the messages */ /* (e.g. with random content and in random time points). */ /* The mailbox is implemented as a list of messages, */ /* i.e. it can contain more messages. */ /* 3) As point 2), but for more consumer threads. Every */ /* consumer uses sleep (random_time) within its loop. */ /**************************************************************/ /* Zakladni priklad na Javu, dve vlakna a monitor. */ /* Monitor realizuje jednoprvkovou schranku */ /* pres kterou si vlakna vymenuji zpravu. */ /* Zprava je pole znaku. Prvni vlakno (producent) cte */ /* retezec z klavesnice a zapisuje do bufferu, druhe vlakno */ /* cte retezec z bufferu a vypisuje na obrazovku. */ /* Napiseme-li: Zhebni potvoro */ /* vlakna ukonci svuj cyklus a program konci. */ /* Namety na modifikace: */ /* 1) predelat do vice souboru, tj. vsechny pouzite tridy */ /* jako verejne. */ /* 2) Producer si zpravy "vymysli" a dava do mail-boxu, */ /* mail-box je implementovan jako dynamicky seznam zprav. */ /* 3) Jako 2, vice consumeru, kazdy z nich da */ /* sleep (nahodna_doba) pred vypisem, cili vypisy */ /* se zamichaji. */ /**************************************************************/ import java.io.*; class InputChar { // funkction to read from keyboard public static char readChar () { try { return (char) System.in.read(); } catch (IOException e) { return ('\uFFFF'); } } } class MailBox { // monitor's class // attributes private StringBuffer message; private boolean empty; // methods public MailBox () {empty = true;} // monitor's constructor synchronized public void write (StringBuffer input) { while (!empty) try { wait (); } catch (InterruptedException e) {} message = input; // !!! it writes only a reference empty = false; notify(); } synchronized public StringBuffer read () { while (empty) try { wait (); } catch (InterruptedException e) {} empty = true; notify(); return message; } } class Producer extends Thread { // Producer's class // attributes MailBox box; // reference to the monitor instance String end_string; // reference to the end-string // methods public Producer (MailBox par_box, String par_string) {box = par_box; end_string = par_string;} // constructor public void run () { // thread's "life" int i; char c; while (true) { try { // for better order of outputs sleep (10); // on the screen } catch (InterruptedException e) {} System.out.println ("Producer: write something and hit enter"); StringBuffer message = new StringBuffer (64); i = 0; c = InputChar.readChar (); while ((i<63) && (c!='\n')) { message.append (c); i++; c = InputChar.readChar (); } box.write (message); // monitor's procedure call if (end_string.equalsIgnoreCase (message.toString())) break; } System.out.println ("Good bye from Producer!"); } } class Consumer extends Thread { // Consumer's class // attributes MailBox box; // reference to the monitor instance String end_string; // reference to the end-string // methods public Consumer (MailBox par_box, String par_string) {box = par_box; end_string = par_string;} // constructor public void run () { // thread's "life" StringBuffer message; while (true) { message = box.read(); // monitor's procedure call if (end_string.equalsIgnoreCase (message.toString())) break; else { System.out.println ("Consumer - what I have got:"); System.out.println (message.toString()); } } System.out.println ("Good by from Consumer!"); } } public class Demo_threads { public static void main (String argv []) { String end_string = new String ("Zhebni potvoro"); MailBox box = new MailBox (); Producer t1 = new Producer (box, end_string); Consumer t2 = new Consumer (box, end_string); System.out.println ("Main program starts threads"); t1.start(); t2.start(); try { t1.join(); // waiting for the producer end } catch (InterruptedException e) {} try { t2.join(); // waiting for the consumer end } catch (InterruptedException e) {} System.out.println ("Main thread finished!"); } }