I'm working on implementing Boulanger's algorithm for synchronization in a Java multithreading application. However, I'm facing an issue where the program doesn't display any output, and the run button in my IDE is stuck in "stop and rerun" mode.
here is my code:
import java.util.ArrayList;
public class Mainst {
public static void main(String[] args) throws InterruptedException {
int numberOfTicketProviders = 4;
ArrayList<TicketProvider> ticketProviders = new ArrayList<TicketProvider>();
Solution solution = new Boulanger(numberOfTicketProviders);
for (int i = 0; i < numberOfTicketProviders; i++) {
ticketProviders.add(new TicketProvider(i, solution));
}
for (int i = 0; i < numberOfTicketProviders; i++) {
ticketProviders.get(i).setName(String.format("TicketProvider_%s", ticketProviders.get(i).id));
ticketProviders.get(i).start();
}
for (int i = 0; i < numberOfTicketProviders; i++) {
ticketProviders.get(i).join();
}
var totalSoldTickets = 0;
for (int i = 0; i < numberOfTicketProviders; i++) {
totalSoldTickets += ticketProviders.get(i).getSoldTickets();
}
System.out.printf("Total Sold Tickets = %s", totalSoldTickets);
}
}
public class TicketProvider extends Thread {
public int id;
private int soldTickets = 0;
private final Solution solution;
public TicketProvider(int id, Solution solution) {
this.id = id;
this.solution = solution;
}
@Override
public void run() {
boolean notFull;
do {
solution.entrySection(this);
notFull = Stadium.getTicket();
solution.exitSection(this);
if (notFull)
soldTickets++;
} while (notFull);
System.out.printf("%s, soldTickets = %s\n", Thread.currentThread().getName(), soldTickets);
}
public int getSoldTickets() {
return soldTickets;
}
}
public class Stadium {
private static int capacity = 10_000;
public static boolean getTicket() {
if (capacity > 0) {
capacity--;
return true;
}
return false;
}
}
import java.util.Arrays;
public class Boulanger implements Solution {
boolean[] choosing;
int[] number;
public Boulanger(int numberOfTicketProviders) {
choosing = new boolean[numberOfTicketProviders];
number = new int[numberOfTicketProviders];
Arrays.fill(choosing, false);
Arrays.fill(number, 0);
}
@Override
public void entrySection(TicketProvider ticketProvider) {
int id = ticketProvider.id;
choosing[id] = true;
number[id] = Arrays.stream(number).max().getAsInt() + 1;
choosing[id] = false;
for (int j = 0; j < choosing.length; j++) {
while (choosing[j]) {
}
while ((number[j] != 0 && (number[j] < number[id] || (number[j] == number[id] && j < id)))) {
}
}
}
@Override
public void exitSection(TicketProvider ticketProvider) {
int id = ticketProvider.id;
number[id] = 0;
}
}
public interface Solution {
public void entrySection(TicketProvider ticketProvider);
public void exitSection(TicketProvider ticketProvider);
}
I suspect there might be an issue with my implementation of Boulanger's algorithm or with the way I'm using the Thread class in Java. Can someone please review my code and provide guidance on how to fix this issue?
I'd appreciate any insights or suggestions to resolve this problem. Thank you!