たくさんの行列ができるお店です。各キューには、サービスを待機できる最大クライアント数があり、キューの数を書き込むことができます。私の店は、私が与えた一定の分後に閉まらなければなりません。問題はそうではないということです。提供されるクライアントを生成し、などを生成するだけです。
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(); と関係があると思います。メインクラスから。
ありがとう、ドラゴス