1. 程式人生 > >哲學家問題(java)的三個解法

哲學家問題(java)的三個解法

對象 trace while exceptio 申請 cond name eat body

//加synchronize進行同步
//釋放資源又很快獲得自身的資源,這樣不妥,吃完的話休息100ms

//每個人先申請編號小的筷子

public class Philosopher implements Runnable {
    int[] fork=new int[5];
    Thread thread1=new Thread(this,"1");
    Thread thread2=new Thread(this,"2");
    Thread thread3=new Thread(this,"3");
    Thread thread4=new Thread(this,"4");
    Thread thread5
=new Thread(this,"5"); public void run() { try { while (true) { if (Thread.currentThread().getName().equals("1")) { while (fork[0]==1) { synchronized (this) { wait(); } } fork[
0]=1; while (fork[1]==1) { synchronized (this) { wait(); } } fork[1]=1; System.out.println("1 eats for 3 seconds"); Thread.sleep(
3000); fork[0]=0; fork[1]=0; synchronized(this) { notifyAll(); } Thread.sleep(100); } else if (Thread.currentThread().getName().equals("2")) { while (fork[1]==1) { synchronized(this) { wait(); } } fork[1]=1; while (fork[2]==1) { synchronized(this) { wait(); } } fork[2]=1; System.out.println("2 eats for 3 seconds"); Thread.sleep(3000); fork[1]=0; fork[2]=0; synchronized(this) { notifyAll(); } Thread.sleep(100); } else if (Thread.currentThread().getName().equals("3")) { while (fork[2]==1) { synchronized(this) { wait(); } } fork[2]=1; while (fork[3]==1) { synchronized(this) { wait(); } } fork[3]=1; System.out.println("3 eats for 3 seconds"); Thread.sleep(3000); fork[2]=0; fork[3]=0; synchronized(this) { notifyAll(); } Thread.sleep(100); } else if (Thread.currentThread().getName().equals("4")) { while (fork[3]==1) { synchronized(this) { wait(); } } fork[3]=1; while (fork[4]==1) { synchronized(this) { wait(); } } fork[4]=1; System.out.println("4 eats for 3 seconds"); Thread.sleep(3000); fork[3]=0; fork[4]=0; synchronized(this) { notifyAll(); } Thread.sleep(100); } else if (Thread.currentThread().getName().equals("5")) { while (fork[0]==1) { synchronized(this) { wait(); } } fork[0]=1; while (fork[4]==1) { synchronized(this) { wait(); } } fork[4]=1; System.out.println("5 eats for 3 seconds"); Thread.sleep(3000); fork[0]=0; fork[4]=0; synchronized(this) { notifyAll(); } Thread.sleep(100); } } } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { Philosopher phi=new Philosopher(); for (int i=0;i<5;i++) phi.fork[i]=0; phi.thread1.start(); phi.thread2.start(); phi.thread3.start(); phi.thread4.start(); phi.thread5.start(); } } //當某個線程試圖等待一個自己並不擁有的對象(O)的監控器或者通知其他線程等待該對象(O)的監控器時,拋出該異常。

//讓剛吃完的一個人阻塞,5根筷子供4個人選,則必有一個人獲得在其左右的兩雙筷子

public class Philosopher1 implements Runnable {
    int[] ifeat=new int[5];
    int[] fork=new int[5];
    int noteat;
    Thread thread1=new Thread(this,"1");
    Thread thread2=new Thread(this,"2");
    Thread thread3=new Thread(this,"3");
    Thread thread4=new Thread(this,"4");
    Thread thread5=new Thread(this,"5");
    public void run() {
        try {
            while (true) {
                if (Thread.currentThread().getName().equals("1")) {
                    while (ifeat[0]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    while (fork[0]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    fork[0]=1;
                    while (fork[1]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    fork[1]=1;
                    System.out.println("1 eats for 3 seconds");
                    Thread.sleep(3000);
                    fork[0]=0;
                    fork[1]=0;
                    ifeat[noteat]=0;
                    noteat=0;
                    ifeat[0]=1;                    
                    synchronized(this) {
                        notifyAll();
                    }
                    Thread.sleep(100);
                }
                else if (Thread.currentThread().getName().equals("2")) {
                    while (ifeat[1]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    while (fork[1]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    fork[1]=1;
                    while (fork[2]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    fork[2]=1;
                    System.out.println("2 eats for 3 seconds");
                    Thread.sleep(3000);
                    fork[1]=0;
                    fork[2]=0;
                    ifeat[noteat]=0;
                    noteat=1;                    
                    ifeat[1]=1;
                    synchronized(this) {
                        notifyAll();
                    }
                    Thread.sleep(100);                
                }
                else if (Thread.currentThread().getName().equals("3")) {
                    while (ifeat[2]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }        
                    while (fork[2]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    fork[2]=1;
                    while (fork[3]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    fork[3]=1;
                    System.out.println("3 eats for 3 seconds");
                    Thread.sleep(3000);
                    fork[2]=0;
                    fork[3]=0;
                    ifeat[noteat]=0;
                    noteat=2;
                    ifeat[2]=1;
                    synchronized(this) {
                        notifyAll();
                    }
                    Thread.sleep(100);            
                }
                else if (Thread.currentThread().getName().equals("4")) {
                    while (ifeat[3]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    while (fork[3]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    fork[3]=1;
                    while (fork[4]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    fork[4]=1;
                    System.out.println("4 eats for 3 seconds");
                    Thread.sleep(3000);
                    fork[3]=0;
                    fork[4]=0;
                    ifeat[noteat]=0;
                    noteat=3;
                    ifeat[3]=1;
                    synchronized(this) {
                        notifyAll();
                    }
                    Thread.sleep(100);
                }
                else if (Thread.currentThread().getName().equals("5")) {
                    while (ifeat[4]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    while (fork[4]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    fork[4]=1;
                    while (fork[0]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    fork[0]=1;
                    System.out.println("5 eats for 3 seconds");
                    Thread.sleep(3000);
                    fork[4]=0;
                    fork[0]=0;
                    ifeat[noteat]=0;
                    noteat=4;
                    ifeat[4]=1;
                    synchronized(this) {
                        notifyAll();
                    }
                    Thread.sleep(100);
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Philosopher1 phi=new Philosopher1();
        for (int i=0;i<5;i++)
            phi.fork[i]=0;
        
        phi.ifeat[0]=1;
        for (int i=0;i<5;i++)
            phi.ifeat[i]=0;
        phi.noteat=0;
        
        phi.thread1.start();
        phi.thread2.start();
        phi.thread3.start();
        phi.thread4.start();
        phi.thread5.start();
    }
}

//只有兩雙筷子都有,才獲取,且同時獲取兩雙筷子

public class Philosopher2 implements Runnable {
    int[] fork=new int[5];
    Thread thread1=new Thread(this,"1");
    Thread thread2=new Thread(this,"2");
    Thread thread3=new Thread(this,"3");
    Thread thread4=new Thread(this,"4");
    Thread thread5=new Thread(this,"5");
    public void run() {
        try {
            while (true) {
                if (Thread.currentThread().getName().equals("1")) {
                    while (fork[0]==1 || fork[1]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    fork[0]=1;
                    fork[1]=1;
                    System.out.println("1 eats for 3 seconds");
                    Thread.sleep(3000);
                    fork[0]=0;
                    fork[1]=0;
                    synchronized(this) {
                        notifyAll();
                    }
                    Thread.sleep(100);
                }
                else if (Thread.currentThread().getName().equals("2")) {
                    while (fork[1]==1 || fork[2]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    fork[1]=1;
                    fork[2]=1;
                    System.out.println("2 eats for 3 seconds");
                    Thread.sleep(3000);
                    fork[1]=0;
                    fork[2]=0;
                    synchronized(this) {
                        notifyAll();
                    }
                    Thread.sleep(100);                
                }
                else if (Thread.currentThread().getName().equals("3")) {
                    while (fork[2]==1 || fork[3]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    fork[2]=1;
                    fork[3]=1;
                    System.out.println("3 eats for 3 seconds");
                    Thread.sleep(3000);
                    fork[2]=0;
                    fork[3]=0;
                    synchronized(this) {
                        notifyAll();
                    }
                    Thread.sleep(100);
                }
                else if (Thread.currentThread().getName().equals("4")) {
                    while (fork[3]==1 || fork[4]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    fork[3]=1;
                    fork[4]=1;
                    System.out.println("4 eats for 3 seconds");
                    Thread.sleep(3000);
                    fork[3]=0;
                    fork[4]=0;
                    synchronized(this) {
                        notifyAll();
                    }
                    Thread.sleep(100);
                }
                else if (Thread.currentThread().getName().equals("5")) {
                    while (fork[0]==1 || fork[4]==1) {
                        synchronized (this) {
                            wait();
                        }
                    }
                    fork[0]=1;
                    fork[4]=1;
                    System.out.println("5 eats for 3 seconds");
                    Thread.sleep(3000);
                    fork[0]=0;
                    fork[4]=0;
                    synchronized(this) {
                        notifyAll();
                    }
                    Thread.sleep(100);
                }            
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Philosopher2 phi=new Philosopher2();
        for (int i=0;i<5;i++)
            phi.fork[i]=0;
        phi.thread1.start();
        phi.thread2.start();
        phi.thread3.start();
        phi.thread4.start();
        phi.thread5.start();
    }
}

哲學家問題(java)的三個解法