移动端多线程编程高级篇-哲学家吃饭问题

来源:互联网 发布:率土之滨数据 编辑:程序博客网 时间:2024/04/27 18:54

1.哲学家就餐问题问题描述

 



2.源代码描述

 

public class DemoPhil {volatile public List<ReentrantLock> mLocks = new ArrayList<ReentrantLock>();List<Philosopher> mList = new ArrayList<Philosopher>();private Object obj = new Object();public DemoPhil() {for(int i = 0;i < 4;i++){mLocks.add(new ReentrantLock());}//四个哲学家mList.add(new Philosopher(2,"饺子",1));mList.add(new Philosopher(1,"刀削面",2));mList.add(new Philosopher(2,"回锅肉",3));mList.add(new Philosopher(2,"米饭",4));// TODO Auto-generated constructor stubfor(int i = 0;i < 4;i++){final int index = i;Thread thread = new Thread("tName-" + i){@Overridepublic void run() {super.run();final Philosopher philosopher = mList.get(index);while(true){if(philosopher.cnt >= philosopher.MAX_CNT){System.out.println("==============================================");break;}else{//尝试获取左边筷子int leftIndex = index;int rightIndex = index + 1;//右边筷子if(rightIndex >= mList.size()){rightIndex = 0;}ReentrantLock leftChopStrick = mLocks.get(leftIndex);ReentrantLock rightChopStrick = mLocks.get(rightIndex);System.out.println("哲学家"+philosopher.number+"尝试锁定筷子" + leftIndex + "," + rightIndex + "");if(!leftChopStrick.isLocked() && !rightChopStrick.isLocked()){System.out.println("哲学家"+philosopher.number+"锁定筷子" + leftIndex + "," + rightIndex + "成功...");leftChopStrick.lock();rightChopStrick.lock();try{try {System.out.println("哲学家" + philosopher.number + " 正在吃" + philosopher.eatSth + " 需要时间:" + (philosopher.eatTime/1000) + "秒");Thread.sleep(philosopher.eatTime);philosopher.cnt++;} catch (InterruptedException e) {e.printStackTrace();}}finally{leftChopStrick.unlock();rightChopStrick.unlock();synchronized (obj) {obj.notifyAll();}}}else{synchronized (obj) {try {System.out.println("哲学家" + philosopher.number + " 没有锁定筷子成功,等待中...");obj.wait();} catch (InterruptedException e) {e.printStackTrace();}System.out.println("哲学家" + philosopher.number + " 由等待筷子中被唤醒...");}}}}System.out.println("哲学家" + philosopher.number + "已经次完饭啦~");}};thread.start();}}public class Philosopher{public long eatTime;//吃饭时间public String eatSth;//吃饭食物public int number;//哲学家编号public int cnt;//吃几碗饭public final int MAX_CNT = 3;public Philosopher(int eatTime,String eatSth,int number){this.eatTime = eatTime * 1000;this.eatSth = eatSth;this.number = number;}}public static void main(String[] args) {new DemoPhil();}}


运行结果:

哲学家1尝试锁定筷子0,1
哲学家1锁定筷子0,1成功...
哲学家1 正在吃饺子 需要时间:2秒
哲学家3尝试锁定筷子2,3
哲学家3锁定筷子2,3成功...
哲学家3 正在吃回锅肉 需要时间:2秒
哲学家4尝试锁定筷子3,0
哲学家4 没有锁定筷子成功,等待中...
哲学家2尝试锁定筷子1,2
哲学家2 没有锁定筷子成功,等待中...
哲学家2 由等待筷子中被唤醒...
哲学家2尝试锁定筷子1,2
哲学家2 没有锁定筷子成功,等待中...
哲学家4 由等待筷子中被唤醒...
哲学家4尝试锁定筷子3,0
哲学家4 没有锁定筷子成功,等待中...
哲学家1尝试锁定筷子0,1
哲学家1锁定筷子0,1成功...
哲学家1 正在吃饺子 需要时间:2秒
哲学家3尝试锁定筷子2,3
哲学家3锁定筷子2,3成功...
哲学家3 正在吃回锅肉 需要时间:2秒
哲学家4 由等待筷子中被唤醒...
哲学家4尝试锁定筷子3,0
哲学家4 没有锁定筷子成功,等待中...
哲学家2 由等待筷子中被唤醒...
哲学家2尝试锁定筷子1,2
哲学家2 没有锁定筷子成功,等待中...
哲学家2 由等待筷子中被唤醒...
哲学家2尝试锁定筷子1,2
哲学家2 没有锁定筷子成功,等待中...
哲学家1尝试锁定筷子0,1
哲学家1锁定筷子0,1成功...
哲学家1 正在吃饺子 需要时间:2秒
哲学家4 由等待筷子中被唤醒...
哲学家4尝试锁定筷子3,0
哲学家4 没有锁定筷子成功,等待中...
哲学家4 由等待筷子中被唤醒...
哲学家4尝试锁定筷子3,0
哲学家4 没有锁定筷子成功,等待中...
哲学家2 由等待筷子中被唤醒...
哲学家2尝试锁定筷子1,2
哲学家2 没有锁定筷子成功,等待中...
哲学家3尝试锁定筷子2,3
哲学家3锁定筷子2,3成功...
哲学家3 正在吃回锅肉 需要时间:2秒
==============================================
哲学家1已经次完饭啦~
哲学家2 由等待筷子中被唤醒...
哲学家2尝试锁定筷子1,2
哲学家4 由等待筷子中被唤醒...
哲学家4尝试锁定筷子3,0
哲学家2 没有锁定筷子成功,等待中...
哲学家4 没有锁定筷子成功,等待中...
==============================================
哲学家4 由等待筷子中被唤醒...
哲学家4尝试锁定筷子3,0
哲学家4锁定筷子3,0成功...
哲学家3已经次完饭啦~
哲学家4 正在吃米饭 需要时间:2秒
哲学家2 由等待筷子中被唤醒...
哲学家2尝试锁定筷子1,2
哲学家2锁定筷子1,2成功...
哲学家2 正在吃刀削面 需要时间:1秒
哲学家2尝试锁定筷子1,2
哲学家2锁定筷子1,2成功...
哲学家2 正在吃刀削面 需要时间:1秒
哲学家2尝试锁定筷子1,2
哲学家2锁定筷子1,2成功...
哲学家2 正在吃刀削面 需要时间:1秒
哲学家4尝试锁定筷子3,0
哲学家4锁定筷子3,0成功...
哲学家4 正在吃米饭 需要时间:2秒
==============================================
哲学家2已经次完饭啦~
哲学家4尝试锁定筷子3,0
哲学家4锁定筷子3,0成功...
哲学家4 正在吃米饭 需要时间:2秒
==============================================
哲学家4已经次完饭啦~