当前位置: 移动技术网 > IT编程>开发语言>Java > 哲学家就餐问题中的JAVA多线程学习

哲学家就餐问题中的JAVA多线程学习

2019年07月22日  | 移动技术网IT编程  | 我要评论

问题描述:一圆桌前坐着5位哲学家,两个人中间有一只筷子,桌子中央有面条。哲学家思考问题,当饿了的时候拿起左右两只筷子吃饭,必须拿到两只筷子才能吃饭。上述问题会产生死锁的情况,当5个哲学家都拿起自己右手边的筷子,准备拿左手边的筷子时产生死锁现象。

解决办法:

1、添加一个服务生,只有当经过服务生同意之后才能拿筷子,服务生负责避免死锁发生。

2、每个哲学家必须确定自己左右手的筷子都可用的时候,才能同时拿起两只筷子进餐,吃完之后同时放下两只筷子。

3、规定每个哲学家拿筷子时必须拿序号小的那只,这样最后一位未拿到筷子的哲学家只剩下序号大的那只筷子,不能拿起,剩下的这只筷子就可以被其他哲学家使用,避免了死锁。这种情况不能很好的利用资源。 

代码实现:实现第2种方案

复制代码 代码如下:

package cn.edu.sdust.philosopher;


/*每个哲学家相当于一个线程*/
class philosopher extends thread{
    private string name;
    private fork fork;
    public philosopher(string name,fork fork){
        super(name);
        this.name=name;
        this.fork=fork;
    }

    public void run(){
        while(true){
            thinking();
            fork.takefork();
            eating();
            fork.putfork();
        }

    }

   
    public void eating(){
        system.out.println("i am eating:"+name);
        try {
            sleep(1000);//模拟吃饭,占用一段时间资源
        } catch (interruptedexception e) {
            // todo auto-generated catch block
            e.printstacktrace();
        }
    }

   
    public void thinking(){
        system.out.println("i am thinking:"+name);
        try {
            sleep(1000);//模拟思考
        } catch (interruptedexception e) {
            // todo auto-generated catch block
            e.printstacktrace();
        }
    }
}

class fork{
    /*5只筷子,初始为都未被用*/
    private boolean[] used={false,false,false,false,false,false};

    /*只有当左右手的筷子都未被使用时,才允许获取筷子,且必须同时获取左右手筷子*/
    public synchronized void takefork(){
        string name = thread.currentthread().getname();
        int i = integer.parseint(name);
        while(used[i]||used[(i+1)%5]){
            try {
                wait();//如果左右手有一只正被使用,等待
            } catch (interruptedexception e) {
                // todo auto-generated catch block
                e.printstacktrace();
            }
        }
        used[i ]= true;
        used[(i+1)%5]=true;
    }

    /*必须同时释放左右手的筷子*/
    public synchronized void putfork(){
        string name = thread.currentthread().getname();
        int i = integer.parseint(name);

        used[i ]= false;
        used[(i+1)%5]=false;
        notifyall();//唤醒其他线程
    }
}

//测试
public class threadtest {

    public static void main(string []args){
        fork fork = new fork();
        new philosopher("0",fork).start();
        new philosopher("1",fork).start();
        new philosopher("2",fork).start();
        new philosopher("3",fork).start();
        new philosopher("4",fork).start();
    }
}

运行结果:

复制代码 代码如下:

i am thinking:0
i am thinking:2
i am thinking:3
i am thinking:1
i am thinking:4
i am eating:0
i am eating:2
i am thinking:0
i am eating:4
i am thinking:2
i am eating:1
i am thinking:4
i am eating:3
i am thinking:1
i am eating:0
i am thinking:3
i am eating:2
i am thinking:0
i am eating:4
i am thinking:2

分析:上述解决方案解决了死锁问题。可以看到最多只能有两条相邻的eating结果,因为每个时刻最多能够满足两个人同时进餐,且两人座位不相邻。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网