Codice PHP:
public class ProducerThread extends Thread{
private BufferMonitor b;
private int i;
public ProducerThread (BufferMonitor b, int m){
this.b=b;
i=m;
}
public void run(){
b.putBuffer(i);
System.out.print(Thread.currentThread()+ " writing " +i+ "\n");
}
}
public class ConsumerThread extends Thread {
private BufferMonitor b;
public ConsumerThread(BufferMonitor b){
this.b=b;
}
public void run(){
System.out.print(Thread.currentThread()+" get"+ b.getBuffer());
}
}
public class MonitorTester {
/**
* @param args
*/
public static void main(String[] args) {
BufferMonitor b=new BufferMonitor();
// TODO Auto-generated method stub
ProducerThread[]p=new ProducerThread[4];
ConsumerThread[]c=new ConsumerThread[3];
for (int i=0; i<4;i++){
p[i]=new ProducerThread(b,i);
p[i].setName("Producer "+i);
try {
p[i].join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.print(e);
}
p[i].start();
}
for (int i=0; i<3;i++){
c[i]=new ConsumerThread(b);
c[i].setName("Consumer "+i );
try{
c[i].join();
}catch (InterruptedException e){
System.out.print(e);
}
c[i].start();
}
}
}