0

我遇到了线程死锁问题。似乎问题在于线程如何工作。我这里有 2 个问题。

  1. 虽然我创建了 5 个线程并启动它们,但并非所有线程都调用run(). 这是随机的,但我不知道为什么。

  2. in main 方法有问题,while()即使我设置了结束条件,我的代码也会无限运行。

如果你能帮助我,那就太好了。

这是我的班级筷子

static class Chopstick {
    public Semaphore mutex = new Semaphore(1);

    void grab() {
        try {
            mutex.acquire();
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }

    void release() {
        mutex.release();
    }
}

这是我的哲学家班(又名线程)。我使用了资源层次的解决方案,奇数哲学家需要先拿起左边的筷子,然后是右边的筷子,偶数拿起右边的筷子。

static class Philosopher extends Thread {
    public boolean eaten = false;
    public int number;
    public Chopstick leftChopstick;
    public Chopstick rightChopstick;
    public Philosopher(int number, Chopstick leftChopstick, Chopstick rightChopstick) {
        this.number = number;
        this.leftChopstick = leftChopstick;
        this.rightChopstick = rightChopstick;
    }
    public void run() {
        int phils=5;
        while (true) {
            if (number % 2 == 1) {
                leftChopstick.grab();
                System.out.println("Philosopher " + number  + " grabs chopstick " + number);
                rightChopstick.grab();
                System.out.println("Philosopher " + number  + " grabs chopstick " + (number+1)%phils);
                eat();
                leftChopstick.release();
                System.out.println("Philosopher " + number  + " releases chopstick " + number );
                rightChopstick.release();
                System.out.println("Philosopher " + number  + " releases chopstick " +(number+1)%phils);
            } else {
                leftChopstick.grab();
                System.out.println("Philosopher " + number  + " grabs chopstick " + (number+1)%phils);
                rightChopstick.grab();
                System.out.println("Philosopher " + number  + " grabs chopstick " + number );
                eat();
                leftChopstick.release();
                System.out.println("Philosopher " + number  + " releases chopstick " + (number+1)%phils);
                rightChopstick.release();
                System.out.println("Philosopher " + number  + " releases chopstick " + number );
            }
        }
    }
    void eat() {
        try {
            int sleepTime = ThreadLocalRandom.current().nextInt(0, 1000);
            System.out.println("Philosopher " + number + " eats for " + sleepTime+"s");
            Thread.sleep(sleepTime);
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }
}

我的主要课程:

import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadLocalRandom;

public class Test {
    static int number = 5;
    static Philosopher phils[] = new Philosopher[number];
    static Chopstick chopstick[] = new Chopstick[number]; 
    public static void main(String args[]) {
        for (int i = 0; i < number; i++) {
            chopstick[i] = new Chopstick();
        }
        for (int i = 0; i < number; i++) {
            if (i % 2 == 0) {
                phils[i] = new Philosopher(i, chopstick[(i + 1) % number], chopstick[i]);
            } else {
                phils[i] = new Philosopher(i, chopstick[i], chopstick[(i + 1) % number]);
            }
            phils[i].start();
        }
        while (true) {
            try {
                boolean isAllEaten = true;
                for (Philosopher p : phils) {
                    if (!p.eaten) {
                        isAllEaten = false;
                        break;
                    }
                }
                if (isAllEaten) {
                    System.out.println("Everyone Eats");
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace(System.out);
            }
        }
        System.out.println("Exit The Program!");
        System.exit(0);
    }  
}
4

0 回答 0