All pastes #978751 Raw Edit

Untitled

public text v1 · immutable
·published 2008-04-09 20:02 UTC
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());





			try{
				this.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{
					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<10;i++){
			Thread2 firstThread = new Thread2(box);
			firstThread.start();
			
			
		}
		
	}
}