Thread.interrupt()不起作用后中断

时间:2013-02-14 05:18:36

标签: java multithreading user-interface awt event-dispatch-thread

以下代码用于具有MultiThreads的SlotMachine GUI。当我点击应用程序中的停止按钮时。我希望每个线程都被中断,进入捕获,打印错误,再继续几次“转弯”然后中断。不幸的是,当按下停止按钮时,它不会进入捕获并且不会中断。实际上看起来数字加快了。有人可以解释一下吗?为什么即使在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("");

        }
    }
}

}

2 个答案:

答案 0 :(得分:3)

每次调用actionPerformed()时,您都在创建新线程。你需要创建一次线程并保存这些引用。

答案 1 :(得分:1)

我认为问题在于事件处理过程。实际上当你按下停止时,actioncommand仍处于“开始”状态。 不要设置标签,请尝试使用

toggleButton.setActionCommand("Stop");

Set actioncommand