thread_notify_wait.htm

package ch4_package;

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
{
Producer producer = new Producer();
try{
producer.start();
new Consumer( producer ).start();
}//try
catch(NumberFormatException e)
{ System.out.println("data was blank");}
}
}
class Producer extends Thread {
int called = 0;
//boolean b=null;
private Vector messages = new Vector();
int counter = 0;
public void run() {
try {
System.out.println("I am run, part of thread --initated a process");
// while ( true ) {
do {
putMessage();
//System.out.println("I am run, part of thread --you need me always");
sleep( 1000 );
called++;
System.out.println("I am run, part of thread --you need me always " +called );

} while(called < 5);
System.out.println("I am run, part of thread--- ran out " +called );
}
catch( InterruptedException e ) { }
}

private synchronized void putMessage()
throws InterruptedException {
//while ( messages.size() == MAXQUEUE )
while (counter < 6){
counter++;
wait();
messages.addElement( new java.util.Date().toString() + " wait synchronized " + counter);
notify();
System.out.println("\tnotifed with nofify()");
}
}
// Called by Consumer
public synchronized String getMessage()
throws InterruptedException {
notify();
while ( messages.size() == 0 )
wait();
String message = (String)messages.firstElement();
messages.removeElement( message );
return message;
}
}
class Consumer extends Thread {
Producer producer;
Consumer(Producer p) {
producer = p;
}
public void run() {
try {
while ( true ) {
String message = producer.getMessage();
System.out.println("Got message: " + message);
sleep( 2000 );
}
}
catch( InterruptedException e ) { }
}
}

 

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);
}

}