我在这个练习中有这个问题,我有3个类,一个Provider(Thread),它不断提供存储在LinkedList中的Integer产品。一旦它达到10级,零售商(线程)就可以全部购买。并且有分配器正在协调线程。 产品显示在JFrame上,然后当我点击停止按钮时,每个线程停止,每个零售商都会告诉他们购买了多少产品。
编辑:忘记提出问题,每次我点击停止按钮,applciation冻结,我甚至无法关闭JFrame窗口,不明白为什么。 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;
}
}
提供者类:
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");
}
}
零售商类:
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");
}
}
主要课程:
public class Main {
public static void main(String[] args) {
Distributor distributor = new Distributor();
distributor.execute();
}
}
答案 0 :(得分:1)
问题是您的Thread
实际上永远不会停止, EDT 也会被阻止。
我建议您使用布尔值来阻止Provider
内的无限循环。
class Provider implements Runnable {
private Distributor distribuidor;
private int total = 0;
private boolean isRunning = true;
public void setIsRunning(boolean bool){
isRunning = bool;
}
public Provider(Distributor distribuidor) {
super();
this.distribuidor = distribuidor;
}
@Override
public void run() {
while (isRunning) {
try {
distribuidor.provide((int) (Math.random() * 10) + 1);
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
}
System.out.println("Provider Interrupted");
}
}
在您的Distributor
课程中,更改以下内容:
private Provider pro = new Provider(this);
private Thread provider = new Thread(pro);
和
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
pro.setIsRunning(false);
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();
}
}
});
答案 1 :(得分:1)
不要在循环中捕获InterruptedException,而是将循环放在try {...}
中提供商类:
public class Provider implements Runnable {
private Distributor distribuidor;
private int total = 0;
public Provider(Distributor distribuidor) {
super();
this.distribuidor = distribuidor;
}
@Override
public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {
distribuidor.provide((int) (Math.random() * 10) + 1);
Thread.sleep(10);
}
} catch (InterruptedException interruptedException) {
}
System.out.println("Provider Interrupted");
}
}
零售商类:
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() {
try {
while (!Thread.currentThread().isInterrupted()) {
total += distributor.sell();
Thread.sleep(10);
}
} catch (InterruptedException interruptedException) {
}
System.out.println("Retailer id: " + id + " bought: " + total + " products");
}
}