gpt4 book ai didi

Java:使用线程的应用程序中的无限循环

转载 作者:行者123 更新时间:2023-12-01 14:43:49 25 4
gpt4 key购买 nike

我有一家商店,里面有很多队列。每个队列都有多个可以等待服务的最大客户端数,我可以写入队列数。我的商店必须在我规定的一定时间后关闭。问题是事实并非如此。它只是生成被服务的客户端并生成等等等等。

import java.util.List;
import java.util.Timer;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

//long startTime = System.currentTimeMillis();
long endTime;
int nrCozi;
int nrClientiMax;

System.out.println("Introduceti cate minute va fi deschis magazinul: ");
Scanner in = new Scanner(System.in);
endTime = in.nextInt();
endTime = System.currentTimeMillis() + endTime * 1000 * 60;

System.out.println("Introduceti numarul de cozi: ");
nrCozi = in.nextInt();

System.out.println("Introduceti numarul maxim de clienti per coada: ");
nrClientiMax = in.nextInt();
System.out.println("clienti" + nrClientiMax);

List<Coada> cozi = CoadaUtil.create(nrCozi, endTime);
CoadaUtil.startCozi(cozi);

Timer timer = new Timer();
timer.schedule(new ClientGenerator(nrClientiMax, cozi), 0, (int) (Math.random()* 10 * 1000));


}

}

这是队列类

import java.util.ArrayList;
import java.util.List;

public class Coada implements Runnable {

private List<Client> clients;
private long closingTime;
private long sleepTime = 0;
private String nume;

public Coada(long closingTime) {
clients = new ArrayList<Client>();
this.closingTime = closingTime;
}

public void setNume(String nume) {
this.nume = nume;
}

public void addClient(Client c) {
clients.add(c);
}

private Client pop() {
if (clients.size() > 0) {
Client c = clients.get(0);
clients.remove(0);
return c;
}
return null;
}

@Override
public void run() {
Client c = this.pop();
long currentTime = System.currentTimeMillis();
while (c != null || currentTime < closingTime) {
if (c != null) {
try {
System.out.println("Sunt coada " + this.nume + " si " +
"tratez clientul " + c.getNume());
Thread.sleep(c.getWaitingTime() * 1000);
} catch (InterruptedException e) {
}
}
currentTime = System.currentTimeMillis();
c = this.pop();
if(c==null){
this.sleepTime+=System.currentTimeMillis() - currentTime;
this.sleepTime++;
}
}
// System.out.println("Coada " + this.nume +" a dormit " + this.sleepTime );

}

public int getClientSize() {
return clients.size();
}

public String getNume() {
return this.nume;
}

public long getSleepTime(){
return this.sleepTime;
}


}

ClientGenerator类

import java.util.List;
import java.util.TimerTask;


public class ClientGenerator extends TimerTask {

private int nrClients;

private List<Coada> cozi;

public ClientGenerator(int i, List<Coada> cozi) {
this.nrClients = i;
this.cozi = cozi;
}

@Override
public void run() {
for (int i = 0; i < nrClients; i++) {
System.out.println("!!!!!!!!!!!!!Client" + i +" din " + nrClients);
Client client = new Client((int) (Math.random() * 10), i + ""); // timp random
System.out.println("Clientul " + client.getNume() + " asteapta " + client.getWaitingTime()*10 + " secunde");

Coada coada = CoadaUtil.gasesteCoadaOptima(cozi);
coada.addClient(client);
System.out.println("Am adaugat clientul " + i + " la" +
" coada " + coada.getNume());
}

}

}

CoadaUtil 类

import java.util.ArrayList;
import java.util.List;


public class CoadaUtil {

private CoadaUtil(){}

public static List<Coada> create(int nrCozi, long endTime) {
List<Coada> cozi = new ArrayList<Coada>();
for (int i = 0; i < nrCozi; i++) {
Coada c = new Coada(endTime);
c.setNume(i + "");
cozi.add(c);
}
return cozi;
}

public static void startCozi(List<Coada> cozi) {
for (int i = 0; i < cozi.size(); i++) {
Thread t = new Thread(cozi.get(i));
t.start();
}
}

public static Coada gasesteCoadaOptima(List<Coada> cozi) {
Coada c = cozi.get(0);
for (Coada coada : cozi) {
if (coada.getClientSize() < c.getClientSize()) {
c = coada;
}
}
return c;
}

}

我的猜测是它与 System.currentTimeMillis(); 有关。来自主类。

谢谢,德拉戈斯

最佳答案

程序不会停止,因为在处理客户端的线程完成很久之后,您的Timer 仍会永远生成客户端。

在处理线程(Coada 对象)完成后退出程序的一种简单方法是使 Timer 成为守护线程:

定时器timer = new Timer(true);

当只有守护线程运行时,JVM 将退出。

来自Timer的java文档:

After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer's task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can take arbitrarily long to occur. By default, the task execution thread does not run as a daemon thread, so it is capable of keeping an application from terminating.

关于Java:使用线程的应用程序中的无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15668673/

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