package ch4_package;
//http://www.oreilly.com/catalog/expjava/excerpt/index.html
import java.io.*;
import java.util.Vector;
public class Test_This {
Test_This tt = new Test_This();
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
//InputStreamReader isr = new InputStreamReader(System.in);
//BufferedReader br = new BufferedReader(isr);
//String file_name ="";
Resource resource = new Resource();
Thread controller = new Thread(new Controller(resource));
Thread[] user = new Thread[3];
for(int i=0; i<user.length; i++) {
user[i] = new Thread(new User(i,resource));
}
controller.start();
for(int i=0; i<user.length; i++ ) {
user[i].start();
}
boolean alive;
out: do {
alive = false;
for( int i=0; i<user.length; i++ ) {
alive |= user[i].isAlive();
}
Thread.currentThread().yield();
} while(alive);
controller.interrupt();
}
}
class Resource {
boolean okToSend = false;
public synchronized void displayOutput(int id, String[] msg ) {
try {
while(!okToSend) {
wait();
}
okToSend = false;
for( int i = 0; i<msg.length; i++) {
Thread.currentThread().sleep((long)1000);
System.out.println(id + ": " + msg[i] );
}
} catch(InterruptedException e) {}
}
public synchronized void allowOutput() {
okToSend = true;
notify();
}
}
class Controller implements Runnable {
Resource resource;
public Controller(Resource resource) {
this.resource = resource;
}
public void run() {
try{
while(true) {
Thread.currentThread().sleep((long)10000);
resource.allowOutput();
}
} catch(InterruptedException e){}
}
}
class User implements Runnable {
String[] msg = { "Java", "is", "hot,", "aromatic,","and", "invigorating."
};
int id;
Resource resource;
public User(int id, Resource resource) {
this.id = id;
this.resource = resource;
}
public void run() {
resource.displayOutput(id, msg);
}
}