rendered paste body
class Thread2 extends Thread {
mailbox box;
Thread2(mailbox box) {
this.box = box;
}
public void run() {
synchronized(box){
for(int i = 0; i<5;i++)
box.assign(Thread.currentThread().getName());
notifyAll();
try{
box.wait();
Thread.currentThread().sleep((long)(Math.random()*100000000));
}catch (InterruptedException e){
System.exit(1);
}
}
}
}
class ThreadClear extends Thread{
mailbox box;
ThreadClear(mailbox box){
this.box = box;
}
public void run(){
synchronized(box){
while(true){
System.out.println(box.erase());
notifyAll();
try{
box.wait();
Thread.currentThread().sleep((long)(Math.random()*100000000));
}catch (InterruptedException e){
System.exit(1);
}
}
}
}
}
class mailbox {
String message;
mailbox(String message){
this.message = message;
}
public void assign(String s){
this.message = s;
}
public String erase(){
String s = message;
message ="";
return s;
}
}
public class Coordination {
public static void main(String args[]) {
mailbox box = new mailbox("");
ThreadClear clear = new ThreadClear(box);
clear.start();
for(int i = 0; i<1;i++){
Thread2 firstThread = new Thread2(box);
firstThread.start();
}
}
}