Production:public class Production { private int size; private int totalSize; public int getTotalSize() { return totalSize; } public int getSize() { return size; } public synchronized void input() { this.size++; totalSize++; System.out.println("production size is " + this.size); System.out.println("production total size is " + this.totalSize); } public synchronized void get() { //try{Thread.sleep(10);}catch(Exception e){} this.size--; System.out.println(Thread.currentThread().getName() + " is running and production size is " + this.size); System.out.println("production total size is " + this.totalSize); } } Producerpublic class Producer implements Runnable { Production p = null; public Producer(Production p) { this.p = p; } @Override public void run() { //int i; while (p.getTotalSize() < 50) { p.input(); } } } Consumerpublic class Consumer implements Runnable { Production p = null; public Consumer(Production p) { this.p = p; } public void run() { while (p.getSize() > 0 && p.getTotalSize() <= 50) { p.get(); } } }
private int size;
private int totalSize;
public int getTotalSize() {
return totalSize;
} public int getSize() {
return size;
} public synchronized void input() {
this.size++;
totalSize++;
System.out.println("production size is " + this.size);
System.out.println("production total size is " + this.totalSize);
} public synchronized void get() {
//try{Thread.sleep(10);}catch(Exception e){}
this.size--;
System.out.println(Thread.currentThread().getName() + " is running and production size is " + this.size);
System.out.println("production total size is " + this.totalSize);
}
}
Producerpublic class Producer implements Runnable { Production p = null; public Producer(Production p) {
this.p = p;
} @Override
public void run() {
//int i;
while (p.getTotalSize() < 50) {
p.input();
}
}
}
Consumerpublic class Consumer implements Runnable { Production p = null; public Consumer(Production p) {
this.p = p;
} public void run() {
while (p.getSize() > 0 && p.getTotalSize() <= 50) {
p.get();
}
}
}
if(size < 1) {
return;
}