gpt4 book ai didi

java - 在 Java 中运行多线程

转载 作者:行者123 更新时间:2023-12-03 12:57:40 25 4
gpt4 key购买 nike

我的任务是模拟几个人的 Activity 。它们中的每一个在某个随机时间执行的 Activity 很少:快(0-5 秒)、中(5-10 秒)、慢(10-20 秒)和非常慢(20-30 秒)。每个人在同一时间独立执行其任务。在新任务开始时,我应该打印它的随机时间,启动任务,然后在时间过去后显示下一个任务的时间并启动它。我已经编写了计算时间的 run() 函数,但现在看起来线程一个接一个地完成,而不是在同一时间完成,或者它们只是以这种方式打印。

public class People{
public static void main(String[] args){
Task tasksA[]={new Task("washing","fast"),
new Task("reading","slow"),
new Task("shopping","medium")};
Task tasksM[]={new Task("sleeping zzzzzzzzzz","very slow"),
new Task("learning","slow"),
new Task(" :** ","slow"),
new Task("passing an exam","slow") };
Task tasksJ[]={new Task("listening music","medium"),
new Task("doing nothing","slow"),
new Task("walking","medium") };

BusyPerson friends[]={ new BusyPerson("Alice",tasksA),
new BusyPerson("Mark",tasksM),
new BusyPerson("John",tasksJ)};

System.out.println("STARTING.....................");
for(BusyPerson f: friends)
(new Thread(f)).start();
System.out.println("DONE.........................");
}
}

class Task {

private String task;
private int time;
private Task[]tasks;

public Task(String t, String s){
task = t;
Speed speed = new Speed();
time = speed.getSpeed(s);
}

public Task(Task[]tab){
Task[]table=new Task[tab.length];
for(int i=0; i < tab.length; i++){
table[i] = tab[i];
}
this.tasks = table;
}
}

class Speed {

private static String[]hows = {"fast","medium","slow","very slow"};
private static int[]maxs = {5000, 10000, 20000, 30000};

public Speed(){
}

public static int getSpeed( String speedString){
String s = speedString;
int up_limit=0;
int down_limit=0;
int time=0;
//get limits of time
for(int i=0; i<hows.length; i++){
if(s.equals(hows[i])){
up_limit = maxs[i];
if(i>0){
down_limit = maxs[i-1];
}
else{
down_limit = 0;
}
}
}
//get random time within the limits
Random rand = new Random();
time = rand.nextInt(up_limit) + down_limit;

return time;
}

}

class BusyPerson implements Runnable {
private String name;
private Task[] person_tasks;
private BusyPerson[]persons;

public BusyPerson(String s, Task[]t){
name = s;
person_tasks = t;
}

public BusyPerson(BusyPerson[]tab){
BusyPerson[]table=new BusyPerson[tab.length];
for(int i=0; i < tab.length; i++){
table[i] = tab[i];
}
this.persons = table;
}

public void run() {
int time = 0;
double t1=0;

for(Task t: person_tasks){
t1 = (double)t.time/1000;
System.out.println(name+" is... "+t.task+" "+t.speed+
" ("+t1+" sec)");
while (time == t.time) {
try {
Thread.sleep(10);
} catch(InterruptedException exc) {
System.out.println("End of thread.");
return;
}
time = time + 100;
}
}

}
}

我的输出:
STARTING.....................
DONE.........................
Mark is... sleeping zzzzzzzzzz very slow (36.715 sec)
Mark is... learning slow (10.117 sec)
Mark is... :** slow (29.543 sec)
Mark is... passing an exam slow (23.429 sec)
Alice is... washing fast (1.209 sec)
Alice is... reading slow (23.21 sec)
Alice is... shopping medium (11.237 sec)
John is... listening music medium (8.263 sec)
John is... doing nothing slow (13.576 sec)
John is... walking medium (11.322 sec)

虽然它应该是这样的:
   STARTING.....................
DONE.........................
John is... listening music medium (7.05 sec)
Alice is... washing fast (3.268 sec)
Mark is... sleeping zzzzzzzzzz very slow (23.71 sec)
Alice is... reading slow (15.516 sec)
John is... doing nothing slow (13.692 sec)
Alice is... shopping medium (8.371 sec)
Mark is... learning slow (13.904 sec)
John is... walking medium (5.172 sec)
Mark is... :** slow (12.322 sec)
Mark is... passing an exam very slow (27.1 sec)

最佳答案

似乎您的等待循环具有错误的终止条件,使用 time == t.time而不是 time <= t.time .此外,它似乎在每次迭代中增加 100 而不是 10。

另外,您应该重置 time在每个循环之前。

换句话说,循环

while (time == t.time) {
try {
Thread.sleep(10);
} catch(InterruptedException exc) {
System.out.println("End of thread.");
return;
}
time = time + 100;
}

应该是
time = 0;
while (time <= t.time) {
try {
Thread.sleep(10);
} catch(InterruptedException exc) {
System.out.println("End of thread.");
return;
}
time += 10;
}

或者改用 for 循环:
for (int time = 0; time <= t.time; time += 10) {
try {
Thread.sleep(10);
} catch(InterruptedException exc) {
System.out.println("End of thread.");
return;
}
}

然而,真的没有任何理由以 10 毫秒的增量 hibernate 。另外, Thread.sleep()可能不准确。最好一次尝试整个时间段,然后检查时钟。
long timeToWakeup = System.currentTimeMillis() + t.time;
long sleepMs = t.time;
while (sleepMs > 0) {
try {
Thread.sleep(sleepMs);
} catch(InterruptedException exc) {
System.out.println("End of thread.");
return;
}
sleepMs = timeToWakeup - System.currentTimeMillis());
}

关于java - 在 Java 中运行多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2617254/

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