0

我正在尝试用 Java 编写一个 SlotMachine 小程序。代码第一次运行,但是当我第二次尝试点击开始按钮时,程序崩溃了。我知道这是线程中断和 Thread.start(); 的问题。我试图实现一个布尔值“isRunning”而不是中断,但这不起作用。如何停止线程然后重新开始?一些见解将非常有帮助。谢谢!

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, result2, result3;
private Button toggleButton;
public static final int HEIGHT = 150;
public static final int WIDTH = 100;

box1Thread counter1Thread = new box1Thread();
box2Thread counter2Thread = new box2Thread();
box3Thread counter3Thread = new box3Thread();

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 {

    Random generator1 = new Random();

    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 randomInt1 = generator1.nextInt(75);
                for (int i = 0; i < randomInt1; i++) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e1) {
                        System.err.println("Interrupted.");
                    }
                    result1 = (count1++) % 4;
                    textWindow1.setText(Integer.toString(result1));
                }
                break;

            }
        }
    }
}

private class box2Thread extends Thread {

    Random generator2 = new Random();

    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 randomInt2 = generator2.nextInt(75);
                for (int i = 0; i < randomInt2; i++) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e1) {
                        System.err.println("Interrupted.");
                    }
                    result2 = (count2++) % 4;
                    textWindow2.setText(Integer.toString(result2));
                }
                break;
            }
        }
    }
}

private class box3Thread extends Thread {

    Random generator3 = new Random();

    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 randomInt3 = generator3.nextInt(75);
                for (int i = 0; i < randomInt3; i++) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e1) {
                        System.err.println("Interrupted.");
                    }
                    result3 = (count3++) % 4;
                    textWindow3.setText(Integer.toString(result3));
                }
                break;
            }
        }
    }
}

private class ToggleButtonListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Stop")) {

            counter1Thread.interrupt();
            counter2Thread.interrupt();
            counter3Thread.interrupt();
            toggleButton.setLabel("Wait");
            try {
                counter1Thread.join();
                counter2Thread.join();
                counter3Thread.join();
            } catch (InterruptedException e1) {
                System.err.println("Interrupted.");
            }
            if ((result1 == result2) && (result2 == result3)) {
                resultWindow.setText("You Win!");
            } else {
                resultWindow.setText("Sorry, You Lose!");
            }
            toggleButton.setLabel("Start");

        }

        else if (e.getActionCommand().equals("Start")) {

            counter1Thread.start();
            counter2Thread.start();
            counter3Thread.start();

            toggleButton.setLabel("Stop");
            resultWindow.setText("");

        }
    }
}

}

这是我收到的错误:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
at SlotMachine$ToggleButtonListener.actionPerformed(SlotMachine.java:166)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
4

2 回答 2

2

如何停止线程然后重新开始?

你不能。当一个线程终止时,它不能被重新启动。

您需要创建一个新线程并启动它......或者不要让前一个线程停止。有关更完整的答案,请参阅链接的问题...

于 2013-02-14T06:57:37.503 回答
2

在您执行的操作中执行以下操作。

else if (e.getActionCommand().equals("Start"))
  {
    counter1Thread = new box1Thread();
    counter2Thread = new box2Thread();
    counter3Thread = new box3Thread();
    counter1Thread.start();
    counter2Thread.start();
    counter3Thread.start();

    toggleButton.setLabel("Stop");
    resultWindow.setText("");

  }

因为一旦线程停止,你就不能再统计了。因此,您要么不让线程停止,要么创建新线程。

在以前的代码中,我正在创建新线程。

于 2013-02-14T07:04:01.637 回答