每次调用
下面为带有多线程的SlotMachine GUI编写代码。当我按下应用程序中的停止按钮时。我希望每个线程都被中断,进入catch,打印一个错误,再继续几圈,然后中断。不幸的是,当按下停止按钮时,它不会进入锁扣,也不会断裂。事实上,数字似乎在加速。有人能放些光吗?为什么线程即使在Thread.interrupt();之后也不中断;?非常感谢。
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
@SuppressWarnings("serial")
public class SlotMachine extends Frame {
private TextField textWindow1, textWindow2, textWindow3, resultWindow;
private int count1 = 0, count2 = 0, count3 = 0, result1 = 0, result2 = 0,
result3 = 0;
private Button toggleButton;
public static final int HEIGHT = 150;
public static final int WIDTH = 100;
Random generator = new Random();
public static void main(String[] args) {
SlotMachine counterWindow = new SlotMachine();
counterWindow.setVisible(true);
}// main
public SlotMachine() {
setSize(WIDTH, HEIGHT);
addWindowListener(new WindowDestroyer());
setTitle("Slot Machine");
setBackground(Color.orange);
setLayout(new FlowLayout());
textWindow1 = new TextField(1);
add(textWindow1);
textWindow1.setText("*");
textWindow2 = new TextField(1);
add(textWindow2);
textWindow2.setText("*");
textWindow3 = new TextField(1);
add(textWindow3);
textWindow3.setText("*");
toggleButton = new Button("Start");
toggleButton.addActionListener(new ToggleButtonListener());
add(toggleButton);
resultWindow = new TextField(10);
add(resultWindow);
resultWindow.setText("");
}
private class box1Thread extends Thread {
public void run() {
while (true) {
try {
Thread.sleep(50);
result1 = (count1++) % 4;
textWindow1.setText(Integer.toString(result1));
} catch (InterruptedException e) {
System.err.println("Interrupted.");
int randomInt = generator.nextInt(25);
for (int i = 0; i < randomInt; i++) {
result1 = (count1++) % 4;
textWindow1.setText(Integer.toString(result1));
}
break;
}
}
}
}
private class box2Thread extends Thread {
public void run() {
while (true) {
try {
Thread.sleep(50);
result2 = (count2++) % 4;
textWindow2.setText(Integer.toString(result2));
} catch (InterruptedException e) {
System.err.println("Interrupted.");
int randomInt = generator.nextInt(25);
for (int i = 0; i < randomInt; i++) {
result2 = (count2++) % 4;
textWindow2.setText(Integer.toString(result2));
}
break;
}
}
}
}
private class box3Thread extends Thread {
public void run() {
while (true) {
try {
Thread.sleep(50);
result3 = (count3++) % 4;
textWindow3.setText(Integer.toString(result3));
} catch (InterruptedException e) {
System.err.println("Interrupted.");
int randomInt = generator.nextInt(25);
for (int i = 0; i < randomInt; i++) {
result3 = (count3++) % 4;
textWindow3.setText(Integer.toString(result3));
}
break;
}
}
}
}
private class ToggleButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
box1Thread counter1Thread = new box1Thread();
box2Thread counter2Thread = new box2Thread();
box3Thread counter3Thread = new box3Thread();
if (e.getActionCommand().equals("Stop")) {
counter1Thread.interrupt();
counter2Thread.interrupt();
counter3Thread.interrupt();
toggleButton.setLabel("Start");
if (result1 == result2 && result2 == result3) {
resultWindow.setText("You Win!");
} else {
resultWindow.setText("Sorry, You Lose!");
}
}
else if (e.getActionCommand().equals("Start")) {
counter1Thread.start();
counter2Thread.start();
counter3Thread.start();
toggleButton.setLabel("Stop");
resultWindow.setText("");
}
}
}
}
actionPerformed()
时,都会创建新的线程。您需要创建一次线程并保存这些引用。
我认为问题出在事件处理过程上。实际上,当您按下停止时,actioncommand仍然处于"开始"。与其设置标签,不如尝试使用
toggleButton.setActionCommand("Stop");
设置操作命令