Проблема обедающие философы

столкнулся с ошибкой можете помочь

import java.util.Random;

public class Philosopher implements Runnable {
private final Random random = new Random();
private final String name;

private final Fork myFork;

private final Fork enemyFrom;

private int satiety;

public Philosopher(String name, Fork myFork, Fork enemyFrom) {
    this.name = name;
    this.myFork = myFork;
    this.enemyFrom = enemyFrom;
}

private void eating() throws InterruptedException {
    int timeEating = random.nextInt(10000);
    Thread.sleep(timeEating);
    System.out.println(timeEating);
    for (int i = 0; i < timeEating; i+=1000) {
        satiety += 2;
    }
    System.out.println("Philosopher " + name + " закончил есть и положил две вилки.");
    myFork.downFork();
    enemyFrom.downFork();
}

private void thinking() throws InterruptedException {
    int timeSleep = random.nextInt(5000);
    Thread.sleep(timeSleep);
    for (int i = 0; i < timeSleep; i+=1000) {
        satiety -= 1;
    }
    System.out.println("Philosopher " + name + " закончил размышление.");
}


@Override
public void run() {
    System.out.println("Philosopher " + name + " сел за стол.");
    while(true) {
        if (!myFork.getTaken() && !enemyFrom.getTaken()) {
            myFork.takeFork();
            System.out.println("Philosopher " + name + " взял одну вилку и размышляет.");
            try {
                thinking();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Philosopher " + name + " взял вторую вилку и начал есть.");
            enemyFrom.takeFork();
            try {
                eating();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (myFork.getTaken() && enemyFrom.getTaken()) {
            System.out.println("Philosopher " + name + " размышляет потому что две вилки 
заняты.");
        }
        if ((myFork.getTaken() && !enemyFrom.getTaken()) || (!myFork.getTaken() && 
enemyFrom.getTaken())) {
            System.out.println("Philosopher " + name + " взял одну вилку и размышляет.");
            enemyFrom.takeFork();
            try {
                thinking();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Philosopher " + name + " не может взять вторую вилку, кладёт 
свою и размышляет.");
            enemyFrom.downFork();
            try {
                thinking();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Philosopher " + name + "  имеет " + satiety + " сытости.");
    }
}

}

**==============================**


public class Fork {
private boolean taking;

public Fork(boolean taking) {
    this.taking = taking;
}

public boolean getTaken() {
    return taking;
}

public void takeFork() {
    this.taking = true;
}

public void downFork() {
    this.taking = false;
}

}

**==============================**

import java.util.ArrayList;

import java.util.Collections; import java.util.List;

public class Main {
public static void main(String[] args) {
    List<String> names = new ArrayList<>(5);
    Collections.addAll(names, "Кант", "Ахмад", "Марк", "Фома", "Ричард");
    List<Fork> forks = new ArrayList<>(5);
    Collections.addAll(forks,
            new Fork(false),
            new Fork(false),
            new Fork(false),
            new Fork(false),
            new Fork(false)
    );

    Thread thread1 = new Thread(new Philosopher(names.get(0), forks.get(0), forks.get(1)));
    Thread thread2 = new Thread(new Philosopher(names.get(1), forks.get(1), forks.get(2)));
    Thread thread3 = new Thread(new Philosopher(names.get(2), forks.get(2), forks.get(3)));
    Thread thread4 = new Thread(new Philosopher(names.get(3), forks.get(3), forks.get(4)));
    Thread thread5 = new Thread(new Philosopher(names.get(4), forks.get(4), forks.get(0)));

    Thread[] threads = {thread1, thread2, thread3, thread4, thread5};

    for (Thread th : threads) {
        th.start();
    }

}
}

Ответы (0 шт):