I got an issue with a multi-threaded program.
I need to simulate a lot of people trying to book the same flight at the same time, using no locks.
So I made an ExecutorService and used that to pool threads so I can have many simultaneous attempt going at once.
The problem is though, the program sort of reaches the end and right before it prints out all the results, it just sits there and runs forever. I've tried to go into all the other classes that utilize a connection to a database and simply close them off manually. No luck.
package dbassignment4;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
*
* @author Vipar
*/
public class Master{
private static final int POOL_SIZE = 50;
public static boolean planeIsBooked = false;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int success = 0;
int seatNotReserved = 0;
int seatNotReservedByCustomerID = 0;
int reservationTimeout = 0;
int seatIsOccupied = 0;
int misc = 0;
HelperClass.clearAllBookings("CR9");
Thread t = new Thread(new DBMonitor("CR9"));
long start = System.nanoTime();
//HelperClass.clearAllBookings("CR9");
ExecutorService pool = new ThreadPoolExecutor(
POOL_SIZE, POOL_SIZE,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(POOL_SIZE));
int threadsStarted = 0;
List<Future<Integer>> results = new ArrayList<>();
long id = 1;
t.start();
while(planeIsBooked == false) {
try {
Future<Integer> submit = pool.submit(new UserThread(id));
results.add(submit);
} catch (RejectedExecutionException ex) {
misc++;
continue;
}
threadsStarted++;
id++;
}
pool.shutdownNow();
int count = 0;
for(Future<Integer> i : results) {
try {
switch(i.get()) {
case 0:
// Success
success++;
break;
case -1:
// Seat is not Reserved
seatNotReserved++;
break;
case -2:
// Seat is not Reserved by Customer ID
seatNotReservedByCustomerID++;
break;
case -3:
// Reservation Timeout
reservationTimeout++;
break;
case -4:
// Seat is occupied
seatIsOccupied++;
break;
default:
misc++;
// Everything else fails
break;
}
} catch (ExecutionException | InterruptedException ex) {
misc++;
}
count++;
System.out.println("Processed Future Objects: " + count);
// HERE IS WHERE IT LOOPS
}
Here is the rest of the code it doesn't execute right after:
long end = System.nanoTime();
long time = end - start;
System.out.println("Threads Started: " + threadsStarted);
System.out.println("Successful Bookings: " + success);
System.out.println("Seat Not Reserved when trying to book: " + seatNotReserved);
System.out.println("Reservations that Timed out: " + reservationTimeout);
System.out.println("Reserved by another ID when trying to book: " + seatNotReservedByCustomerID);
System.out.println("Bookings that were occupied: " + seatIsOccupied);
System.out.println("Misc Errors: " + misc);
System.out.println("Execution Time (Seconds): " + (double) (time / 1000000000));
}
}
Can you spot the issue? I put in a comment where the code stops running.