I have this problem on this exercise where I have 3 classes, a Provider(Thread) that keeps providing Integer products stored inside a LinkedList. Once it reaches size 10 at least, Retailers(Thread) can buy them all. And there is the Distributor which is coordinating the threads. The products are displayed on a JFrame, and then when I click the Stop button, every thread stops and each retailer tells how many products they bought.
EDIT: Forgot to put the problem, everytime I click on the Stop button, the applciation freezes and I cant even close the JFrame window, dont understand why.
public class Distributor {
private JTextField textfield = new JTextField();
private LinkedList<Integer> productList = new LinkedList<Integer>();
private JFrame frame = new JFrame("Window");
private JButton btn = new JButton("Stop");
private Thread provider = new Thread(new Provider(this));
private LinkedList<Thread> retailerList = new LinkedList<Thread>();
private void addRetailer(int num) {
for (int i = 0; i < num; i++)
retailerList.add(new Thread(new Retailer(i, this)));
}
public Distributor() {
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(textfield);
frame.add(btn, BorderLayout.SOUTH);
addRetailer(2);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
provider.interrupt();
provider.join();
System.out.println(provider.isAlive());
for (Thread t : retailerList) {
t.interrupt();
t.join();
System.out.println(t.isAlive());
}
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
});
}
public void execute() {
frame.setVisible(true);
provider.start();
for (Thread t : retailerList)
t.start();
}
// Keeps providing products, and notifies retailers when there are 10 products
public synchronized void provide(int product) {
textfield.setText(productList.toString());
productList.add(product);
if (productList.size() == 10)
notifyAll();
}
// Sells all the products if there are at least 10 to sell.
public synchronized int sell() throws InterruptedException {
while (productList.size() < 10)
wait();
int total = productList.size();
notifyAll();
textfield.setText(productList.toString());
productList.clear();
return total;
}
}
The Provider class:
public class Provider implements Runnable {
private Distributor distribuidor;
private int total = 0;
public Provider(Distributor distribuidor) {
super();
this.distribuidor = distribuidor;
}
@Override
public void run() {
while (!Thread.interrupted()) {
try {
distribuidor.provide((int) (Math.random() * 10) + 1);
Thread.sleep(10);
} catch (Exception e) {
// TODO: handle exception
}
}
System.out.println("Provider Interrupted");
}
}
Retailer class:
public class Retailer implements Runnable {
private Distributor distributor;
private int total = 0;
private int id;
public Retailer(int id, Distributor distributor) {
super();
this.id = id;
this.distributor = distributor;
}
@Override
public void run() {
while (!Thread.interrupted()) {
try {
total += distributor.sell();
Thread.sleep(10);
} catch (Exception e) {
// TODO: handle exception
}
}
System.out.println("Retailer id: " + id + " bought: " + total + " products");
}
}
And the main class:
public class Main {
public static void main(String[] args) {
Distributor distributor = new Distributor();
distributor.execute();
}
}