操作系统题目,可以用java实现一下嘛!!!急!!!

MrStub 2018-10-30 02:54:44
桌上有一只盘子,每次只能放入一只水果;爸爸专向盘子中放苹果(apple),妈妈专向盘子中放桔于(orange),一个儿子专等吃盘子中的桔子,一个女儿专等吃盘子里的苹果
...全文
223 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Coder米 2018-11-08
  • 打赏
  • 举报
回复
生产者消费者的问题吧,我也是初学者
qq_39936465 2018-11-01
  • 打赏
  • 举报
回复
public class ThreadDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Fruits fruits = new Fruits();
		Putfruit p1 = new Putfruit(fruits);
		Takefruit tf1 = new Takefruit(fruits);
		Thread td1 = new Thread(p1, "Father");
		Thread td2 = new Thread(p1, "Mother");
		Thread td3 = new Thread(tf1, "Son");
		Thread td4 = new Thread(tf1, "Sister");
		td1.start();
		td2.start();
		td3.start();
		td4.start();

	}

}

class Fruits {

	private String fruit;
	private static boolean isFull = false;
	private int count = 0;

	public String getFruit() {
		return fruit;
	}

	public void setFruit(String fruit) {
		this.fruit = fruit;
	}

	

	public static boolean isFull() {
		return isFull;
	}

	public static void setFull(boolean isFull) {
		Fruits.isFull = isFull;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public synchronized void add() {

		try {
			while (isFull()) {
				wait();
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (getCount() == 0) {
			if (Thread.currentThread().getName().equals("Father")) {
				setFruit("Apple");
				setCount(1);
				setFull(true);
				System.out.println(Thread.currentThread().getName()+"放了一个"+getFruit());
				notifyAll();
			} else {
				setFruit("Orange");
				setCount(1);
				setFull(true);
				System.out.println(Thread.currentThread().getName()+"放了一个"+getFruit());
				notifyAll();
			}

		}

	}

	public synchronized void eat() {

		while (!isFull()) {
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (getCount() == 1) {
			
			if (Thread.currentThread().getName().equals("Son") && getFruit().equals("Apple")) {
				System.out.println(Thread.currentThread().getName()+"吃了一个"+getFruit());
				setFruit(null);
				setFull(false);
				setCount(0);
				notifyAll();
			}else if (Thread.currentThread().getName().equals("Sister") && getFruit().equals("Orange")) {
				System.out.println(Thread.currentThread().getName()+"吃了一个"+getFruit());
				setFruit(null);
				setFull(false);
				setCount(0);
				notifyAll();
			}
				
			
		}
	}

}

 class Putfruit implements Runnable {

	private Fruits fruits;

	public Putfruit(Fruits fruits) {
		super();
		this.fruits = fruits;
	}

	public void run() {
		while(true) {
			fruits.add();
		}

	}

}
 class Takefruit implements Runnable {

	private Fruits fruits;

	public Takefruit(Fruits fruits) {
		super();
		this.fruits = fruits;
	}

	public void run() {
		while(true){
			fruits.eat();
		}
	}

}
游一游走一走 2018-11-01
  • 打赏
  • 举报
回复
希望可以帮到你.

    @org.junit.Test
    public void test() throws InterruptedException {
        BlockingDeque apple = new LinkedBlockingDeque();
        BlockingDeque orange = new LinkedBlockingDeque();
        new Thread(() -> {
            int i = 0;
            while (true) {
                String data = "爸爸放苹果" + (++i);
                apple.push(data);
                System.out.println(data + " 已经完成,开始随机休息");
                try {
                    TimeUnit.MICROSECONDS.sleep(new Random().nextInt(1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(() -> {
            int i = 0;
            while (true) {
                String data = "妈妈放橘子" + (++i);
                orange.push(data);
                System.out.println(data + " 已经完成,开始随机休息");
                try {
                    TimeUnit.MICROSECONDS.sleep(new Random().nextInt(1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(() -> {
            while (true) {
                try {
                    System.out.println("女儿吃苹果" + apple.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(() -> {
            while (true) {
                try {
                    System.out.println("儿子吃橘子" + orange.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        TimeUnit.MINUTES.sleep(5);
    }

qq_39936465 2018-11-01
  • 打赏
  • 举报
回复
修改了一下程序,添加了一个turn变量 进行轮次判断,使进程都能正常结束。

public class ThreadDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Fruits fruits = new Fruits();
		Putfruit p = new Putfruit(fruits);
		Takefruit t = new Takefruit(fruits);
		Thread td1 = new Thread(p, "Father");
		Thread td2 = new Thread(p, "Mother");
		Thread td3 = new Thread(t, "Son");
		Thread td4 = new Thread(t, "Sister");
		td1.start();
		td2.start();
		td3.start();
		td4.start();
	}

}

class Fruits {

	private String fruit;
	private static boolean isFull = false;
	private int count = 0;
	private int turn = 10;

	public String getFruit() {
		return fruit;
	}

	public void setFruit(String fruit) {
		this.fruit = fruit;
	}

	public static boolean isFull() {
		return isFull;
	}

	public static void setFull(boolean isFull) {
		Fruits.isFull = isFull;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getTurn() {
		return turn;
	}

	public void setTurn() {
		turn--;
	}

	public synchronized void add() {

		try {
			while (isFull()&& getTurn() > 0) {
				wait();
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (getCount() == 0 && getTurn() > 0) {
			if (Thread.currentThread().getName().equals("Father")) {
				setFruit("Apple");
				setCount(1);
				setFull(true);
				System.out.println(Thread.currentThread().getName() + "放了一个" + getFruit());
				notifyAll();
			} else {
				setFruit("Orange");
				setCount(1);
				setFull(true);
				System.out.println(Thread.currentThread().getName() + "放了一个" + getFruit());
				notifyAll();
			}

		}

	}

	public synchronized void eat() {

		while (!isFull()&& getTurn() > 0) {
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (getCount() == 1&& getTurn() > 0 ) {

			if (Thread.currentThread().getName().equals("Son") && getFruit().equals("Apple")) {
				System.out.println(Thread.currentThread().getName() + "吃了一个" + getFruit());
				setFruit(null);
				setFull(false);
				setCount(0);
				setTurn();
				notifyAll();
			} else if (Thread.currentThread().getName().equals("Sister") && getFruit().equals("Orange")) {
				System.out.println(Thread.currentThread().getName() + "吃了一个" + getFruit());
				setFruit(null);
				setFull(false);
				setCount(0);
				setTurn();
				notifyAll();
			}

		}
	}

}

class Putfruit implements Runnable {

	private Fruits fruits;

	public Putfruit(Fruits fruits) {
		super();
		this.fruits = fruits;
	}
	public void run() {
		while(fruits.getTurn()!=0) {
			fruits.add();
		}
		System.out.println("进程" + Thread.currentThread().getName() + "结束了");
	}

}

class Takefruit implements Runnable {

	private Fruits fruits;

	public Takefruit(Fruits fruits) {
		super();
		this.fruits = fruits;
	}

	public void run() {
		while(fruits.getTurn()!=0){
			fruits.eat();
		}
		System.out.println("进程" + Thread.currentThread().getName() + "结束了");
	}

}

运行结果

Father放了一个Apple
Son吃了一个Apple
Mother放了一个Orange
Sister吃了一个Orange
Father放了一个Apple
Son吃了一个Apple
Mother放了一个Orange
Sister吃了一个Orange
Father放了一个Apple
Son吃了一个Apple
Mother放了一个Orange
Sister吃了一个Orange
Father放了一个Apple
Son吃了一个Apple
Mother放了一个Orange
Sister吃了一个Orange
Father放了一个Apple
Son吃了一个Apple
Mother放了一个Orange
Sister吃了一个Orange
进程Sister结束了
进程Mother结束了
进程Son结束了
进程Father结束了

北飞的企鹅 2018-10-31
  • 打赏
  • 举报
回复
这就是多线程生产者消费者
十八道胡同 2018-10-30
  • 打赏
  • 举报
回复
核心是多线程对盘子这个单一资源的锁处理。

synchronized是一种方式。。


学习楼上。
miaoch 2018-10-30
  • 打赏
  • 举报
回复
实际上,当家长放入水果时,应该提醒子女执行拿水果的行为,而子女拿完水果后,再给家长发信号要放水果了,这样才能达到最大的效率。 所以应该还需要用到wait和notify。楼主你加油
miaoch 2018-10-30
  • 打赏
  • 举报
回复

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test {
	public static void main(String[] args) {
		People father = new People(new Runnable() {
			@Override
			public void run() {
				while (true) {
					synchronized (Table.dish) {//获得盘子的控制权
						if (Table.dish.getFood() == null) {
							Table.dish.setFood(new Apple());
							System.out.println("爸爸放了一个苹果...");
						}
					}
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});
		People mother = new People(new Runnable() {
			@Override
			public void run() {
				while (true) {
					synchronized (Table.dish) {//获得盘子的控制权
						if (Table.dish.getFood() == null) {
							Table.dish.setFood(new Orange());
							System.out.println("妈妈放了一个橘子...");
						}
					}
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				
			}
		});
		People daughter = new People(new Runnable() {
			@Override
			public void run() {
				while (true) {
					synchronized (Table.dish) {//获得盘子的控制权
						if (Table.dish.getFood() instanceof Apple) {
							Table.dish.getFood().eat("daughter");
							Table.dish.clear();
						}
					}
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				
			}
		});
		People son = new People(new Runnable() {
			@Override
			public void run() {
				while (true) {
					synchronized (Table.dish) {//获得盘子的控制权
						if (Table.dish.getFood() instanceof Orange) {
							Table.dish.getFood().eat("son");
							Table.dish.clear();
						}
					}
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				
			}
		});
		ExecutorService exec = Executors.newCachedThreadPool();
		exec.execute(father);
		exec.execute(son);
		exec.execute(daughter);
		exec.execute(mother);
	}
}
class People implements Runnable {
	private Runnable runnable;
	public People(Runnable runnable) {
		this.runnable = runnable;
	}
	@Override
	public void run() {
		runnable.run();
	}
}
class Table {
	public final static Dish dish = new Dish();
	private Table() {}
}
class Dish {
	private Edible food;
	public Edible getFood() {
		return food;
	}
	public void setFood(Edible food) {
		this.food = food;
	}
	public void clear() {
		this.food = null;
	}
}
class Orange implements Edible {
	@Override
	public void eat(String who) {
		System.out.println(who + " eat orange");
	}
}
class Apple implements Edible {
	@Override
	public void eat(String who) {
		System.out.println(who + " eat apple");
	}
}
interface Edible {
	void eat(String who);
}
无聊写写的,随便看看吧
qybao 2018-10-30
  • 打赏
  • 举报
回复
这个操作系统没关系吧 爸爸妈妈儿子女儿4个线程的同步问题
十八道胡同 2018-10-30
  • 打赏
  • 举报
回复
题目没说完呀,然后呢

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧