I am trying to create a system where it has a number of queued slots and it accepts X incoming services. Now each service arrives/departs at different times so i could have one arrive 10 seconds from now and departs 20 seconds after arrival while the one after that could arrive sooner/later. Is the best way to implement this is to have 2 threads - one that is going through a loop to clear the queue while another thread is going through another loop generating new services? It is quite possible a new service is generated while another service is being serviced.:
Thread 1:
while(X>0) { //has more incoming services
masterTime++;
if (currentServiceFinishTime > masterTime) {
} else {
removeCurrentService();
startNextService();
}
}
thread 2:
generateNextServiceArrivalTime() {
while (X>0) {
getNextService();
if (queueHasRoom) {
addNextService();
} else if(arrivalTime +currentTime >currentServiceTime) {
removeCurrentService();
initiateNextServiceInQueue();
updateMasterTime();
addNextService();
} else {
rejectIncomingService(); //queue is full and the current service is not going to be done before the incoming service arrives
}
}
is this way of doing it too confusing? any better way to implement this? thanks!