public static void main(String[] args) {
Plate q = new Plate();
Father f1 = new Father(q);
Father f2 = new Father(q);
Father f3 = new Father(q);
Father f4 = new Father(q);
Mother m1 = new Mother(q);
Mother m2 = new Mother(q);
Mother m3 = new Mother(q);
Mother m4 = new Mother(q);
Son s1 = new Son(q);
Son s2 = new Son(q);
Son s3 = new Son(q);
Son s4 = new Son(q);
Daughter d1 = new Daughter(q);
Daughter d2 = new Daughter(q);
Daughter d3 = new Daughter(q);
Daughter d4 = new Daughter(q);
public void run() {
while (true) {
int apple = q.putApple();
System.out.println(Thread.currentThread().getName()
+ " put one apple: the number of apple in the plate:"
+ apple);
}
}
}
class Mother extends Thread {
Plate q;
Mother(Plate q) {
this.q = q;
}
public void run() {
while (true) {
int orange= q.putOrange();
System.out.println(Thread.currentThread().getName()
+ " put one orange:the number of orange in the plate:"
+ orange);
}
}
}
class Son extends Thread {
Plate q;
Son(Plate q) {
this.q = q;
}
public void run() {
while (true) {
System.out.println(Thread.currentThread().getName()
+ " get one orange:the number of orange in the plate:"
+ q.getOrange());
}
}
}
class Daughter extends Thread {
Plate q;
Daughter(Plate q) {
this.q = q;
}
public void run() {
while (true) {
System.out
.println(Thread.currentThread().getName()
+ "Daughter get one apple:the number of apple in the plate:"
+ q.getApple());
}
}
}
class Plate {
private int apple = 0;
private int orange = 0;
private int pFull = 10;
class Father extends Thread
{
Fruits fruit;
Father(Fruits fruit)
{
this.fruit = fruit;
}
public void run()
{
while(true)
{
fruit.setApple();
System.out.println(Thread.currentThread().getName() +
" put one apple: the number of apple in the plate:" + fruit.appleNumber +
"the number of full in the plate:" + fruit.full);
}
}
}
public void run()
{
while(true)
{
fruit.setOrange();
System.out.println(Thread.currentThread().getName() +
" put one oranger: the number of orange in the plate:" + fruit.orangeNumber +
"the number of full in the plate:" + fruit.full);
}
}
}
class Son extends Thread
{
Fruits fruit;
Son(Fruits fruit)
{
this.fruit = fruit;
}
public void run()
{
while(true)
{
fruit.getOrange();
System.out.println(Thread.currentThread().getName() +
" get one orange: the number of orange in the plate:" + fruit.orangeNumber +
"the number of full in the plate:" + fruit.full);
}
}
}
public void run()
{
while(true)
{
fruit.getApple();
System.out.println(Thread.currentThread().getName() +
" get one apple: the number of apple in the plate:" + fruit.appleNumber +
"the number of full in the plate:" + fruit.full);
}
}
}
class Fruits
{
int appleNumber = 0;
int orangeNumber = 0;
int full = 0;
public synchronized int setApple()
{
if(full < 10 && full >= 0 && appleNumber < 10)
{
appleNumber += 1;
full++;
}
return appleNumber;
}
public synchronized int getApple()
{
if(full >= 1 && full <=10 && appleNumber >= 1)
{
appleNumber -= 1;
full--;
}
return appleNumber;
}
public synchronized int setOrange()
{
if(full < 10 && full >= 0 && orangeNumber < 10)
{
orangeNumber += 1;
full++;
}
return orangeNumber;
}
public synchronized int getOrange()
{
if(full >= 1 && full <=10 && orangeNumber >= 1)
{
orangeNumber -= 1;
full--;
}
return orangeNumber;
}
public synchronized int getFull()
{
return appleNumber + orangeNumber;
}
}