gpt4 book ai didi

java - 线程不同时运行

转载 作者:行者123 更新时间:2023-12-02 04:44:14 25 4
gpt4 key购买 nike

我希望这不是一个重复的问题,但我已经查看了其他问题中的所有答案,但没有一个能满足我的问题。

我有一个程序可以解决哲学家就餐的问题,当我运行该程序时,线程会等到下一个线程完成后再运行另一个线程。这会导致线程的输出如下所示:

Philosopher 1 is EATING.
Philosopher 1 is THINKING.
Philosopher 5 is EATING.
Philosopher 5 is THINKING.
Philosopher 3 is EATING.
Philosopher 3 is THINKING.

...等等。预期输出没有顺序。线程应该同时运行。这是我的代码,所有内容都在这里,界面仅指定 DINERS (5) 的大小和 State._______ 是一个具有 3 个状态的枚举:State.HUNGRY、State.THINKING 和 State.EATING。

import java.lang.Runnable;                                                      
import java.util.concurrent.locks.*;
import java.util.Random;
import java.lang.Thread;
import java.util.concurrent.TimeUnit;
/**
* This class handles the Philosophers, I hope they are hungry.
*
* @version 4-20-15
*/
public class Diner implements Runnable, PhilosopherInterface {

/** The lock used to control Thread access */
private final ReentrantLock lock;
/** The state that the Philosopher is in (ex: Eating, Thinking etc.) */
private State current;
/** The random number used to generate time sleeping */
private Random timeGenerator;
/** The maximum time a thread can sleep */
private final int maxTimeToSleep = 5000;
/** The minimum time a thread can sleep (1ms) */
private final int minTimeToSleep = 1;
private int philNum;
private int philIndex;
private Condition[] condition;
private State[] states;

public Diner(ReentrantLock lock, int philNumber, Condition[] condition, State[] states)

philNum = philNumber;
philIndex = philNum - 1;
current = states[philNumber-1];
timeGenerator = new Random();
this.lock = lock;
this.condition = condition;
this.condition[philIndex] = lock.newCondition();
this.states = states;
states[philIndex] = State.THINKING;


}

@Override
public void takeChopsticks() {

states[philIndex] = State.HUNGRY;
lock.lock();
try{
int left = philIndex-1;
int right = philIndex+1;
if(philNum == DINERS) right = 0;
if(philNum == 1) left = DINERS - 1;
test(left, philIndex, right);
if(states[philIndex] != State.EATING) {
condition[philIndex].await();
}
}catch(InterruptedException e){}

}

@Override
public void replaceChopsticks() {
try{
states[philIndex] = State.THINKING;
int left = philIndex-1;
int right = philIndex+1;
if(philNum == DINERS) right = 0;
if(philNum == 1) left = DINERS - 1;
int leftOfLeft = left-1;
int rightOfRight = right+1;
if(left == 0) leftOfLeft = DINERS-1;
test(leftOfLeft, left, philIndex);
if(right == DINERS-1) rightOfRight = 0;
test(philIndex, right, rightOfRight);
}finally{ lock.unlock(); }
//states[philIndex] = State.THINKING;
//condition[left].signal();
//condition[right].signal();
}



public void think() {
System.out.println("Philosopher " + philNum + " is " + State.THINKING + ".");
int timeToSleep = timeGenerator.nextInt(maxTimeToSleep) + minTimeToSleep;
try {
Thread.sleep(500);
}catch(InterruptedException e) {}
}

public void eat() {

System.out.println("Philosopher " + philNum + " is " + State.EATING + ".");
int timeToSleep = timeGenerator.nextInt(maxTimeToSleep) + minTimeToSleep;
try {
Thread.sleep(500);
}catch(InterruptedException e){}
}

@Override
public void run() {

while(true) {

think();
takeChopsticks();
eat();
replaceChopsticks();
}
}

public State getState() {
return current;
}

private void test(int left, int current, int right) {
if(states[left] != State.EATING && states[current] == State.HUNGRY
&& states[right] != State.EATING) {
states[current] = State.EATING;
condition[current].signal();
}
}
}

为什么踏板不能同时运行?谢谢您的帮助!编辑:要运行它,有一个驱动程序是这样的:

public class Lunch {                                                            

public static void main(String[] args) {

ReentrantLock lock = new ReentrantLock();
Thread[] diners = new Thread[PhilosopherInterface.DINERS];
Condition[] table = new Condition[PhilosopherInterface.DINERS];
State[] states = new State[PhilosopherInterface.DINERS];
for(int i=0; i<PhilosopherInterface.DINERS; i++) {
states[i] = State.THINKING;
}


for(int i=0; i<PhilosopherInterface.DINERS; i++) {
Diner diner = new Diner(lock, i+1, table, states);
diners[i] = new Thread(diner);
diners[i].start();

}



}

}

EDIT2:解决了问题,答案如下。

最佳答案

告诉你的线程等待并不强制它们同时工作。如果一个线程需要在另一个线程激活之前执行几个步骤,那么这些方法(步骤)应该同步。

关于java - 线程不同时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29808119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com