哲學家就餐問題中的JAVA多線程學習_java

來源:互聯網
上載者:User

問題描述:一圓桌前坐著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結果,因為每個時刻最多能夠滿足兩個人同時進餐,且兩人座位不相鄰。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.