如何暂停/恢复 Java 线程
我正在用java制作井字棋程序,因为我正在学习java,并且我认为一个简单的项目将是一个很好的起点。这是我到目前为止的代码:
public class Start {
public static void main(String[] args) {
GameTicTacToe gameTicTacToe = new GameTicTacToe();
gameTicTacToe.windowBirth();
}
}
并且,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GameTicTacToe implements ActionListener {
private int gridSize = 3;
private JButton[] gridButton = new JButton[(gridSize * gridSize)];
private JPanel grid = new JPanel(new GridLayout(gridSize, gridSize, 0, 0));
private JFrame windowTicTacToe = new JFrame("Tisk, Task, Toes");
private int[] gridButtonOwner = new int[(gridSize * gridSize)];
private int turn = 1;
private int HolerHor, HolerVer, HolerDia1, HolerDia2;
Thread winnerBlue = new Thread() {
public void run() {
for (int a = 0; a < 4; a++) {
for (int i = 0; i < gridButton.length; i++) {
gridButton[i].setBackground(Color.BLUE);
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
}
for (int i = 0; i < gridButton.length; i++) {
gridButton[i].setBackground(Color.WHITE);
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
}
}
for (int i = 0; i < gridButton.length; i++) {
gridButton[i].setEnabled(true);
gridButtonOwner[i] = 0;
}
}
};
Thread winnerRed = new Thread() {
public void run() {
for (int a = 0; a < 4; a++) {
for (int i = 0; i < gridButton.length; i++) {
gridButton[i].setBackground(Color.RED);
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
}
for (int i = 0; i < gridButton.length; i++) {
gridButton[i].setBackground(Color.WHITE);
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
}
}
for (int i = 0; i < gridButton.length; i++) {
gridButton[i].setEnabled(true);
gridButtonOwner[i] = 0;
}
}
};
public void windowBirth() {
for (int i = 0; i < gridButton.length; i++) {
gridButtonOwner[i] = 0;
gridButton[i] = new JButton("");
gridButton[i].addActionListener(this);
gridButton[i].setBackground(Color.WHITE);
grid.add(gridButton[i]);
}
windowTicTacToe.setDefaultCloseOperation(3);
windowTicTacToe.setLocation(400, 200);
windowTicTacToe.setPreferredSize(new Dimension(400, 400));
windowTicTacToe.add(grid);
windowTicTacToe.pack();
windowTicTacToe.setVisible(true);
}
public void actionPerformed(ActionEvent gridButtonClicked) {
for (int i = 0; i < gridButton.length; i++) {
if (gridButtonClicked.getSource() == gridButton[i]) {
if (turn == 1) {
turn = 2;
gridButtonOwner[i] = 1;
gridButton[i].setBackground(Color.blue);
gridButton[i].setEnabled(false);
} else {
turn = 1;
gridButtonOwner[i] = 2;
gridButton[i].setBackground(Color.red);
gridButton[i].setEnabled(false);
}
}
}
checkWinner();
}
public void checkWinner() {
for (int a = 1; a < 3; a++) {
HolerDia1 = a;
HolerDia2 = a;
for (int b = 0; b < gridSize; b++) {
HolerHor = a;
HolerVer = a;
for (int c2 = 0; c2 < gridSize; c2++) {
HolerHor = (HolerHor * gridButtonOwner[((b * gridSize) + c2)])/ a;
HolerVer = (HolerVer * gridButtonOwner[((c2 * gridSize) + b)])/ a;
}
if (HolerHor == a || HolerVer == a) {
winnerAnimation(a);
}
}
for(int h = 0;h < gridSize; h++){
HolerDia1 = (HolerDia1 * gridButtonOwner[h * (gridSize + 1)]) / a;
HolerDia2 = (HolerDia2 * gridButtonOwner[(h * (gridSize - 1)) + (gridSize - 1)]) / a;
}
if (HolerDia1 == a || HolerDia2 == a) {
winnerAnimation(a);
}
}
}
public void winnerAnimation(int b) {
for (int i = 0; i < gridButton.length; i++) {
gridButton[i].setEnabled(false);
}
if (b == 1){
winnerBlue.start();
}else{
winnerRed.start();
}
}
}
这是我的问题,当玩家获胜时,例如玩家 1(蓝色),它会播放动画(使棋盘闪烁蓝色)。但当玩家 1 再次获胜时,程序崩溃了。
我稍微研究了一下,发现你不能启动一个线程两次,因为你就是不能。
我将如何暂停线程,然后在需要时重新启动它?
I am making a Tic Tac Toe Program in java because i am learning java and i thought a simple project would be a great place to start. This is my code so far:
public class Start {
public static void main(String[] args) {
GameTicTacToe gameTicTacToe = new GameTicTacToe();
gameTicTacToe.windowBirth();
}
}
And,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GameTicTacToe implements ActionListener {
private int gridSize = 3;
private JButton[] gridButton = new JButton[(gridSize * gridSize)];
private JPanel grid = new JPanel(new GridLayout(gridSize, gridSize, 0, 0));
private JFrame windowTicTacToe = new JFrame("Tisk, Task, Toes");
private int[] gridButtonOwner = new int[(gridSize * gridSize)];
private int turn = 1;
private int HolerHor, HolerVer, HolerDia1, HolerDia2;
Thread winnerBlue = new Thread() {
public void run() {
for (int a = 0; a < 4; a++) {
for (int i = 0; i < gridButton.length; i++) {
gridButton[i].setBackground(Color.BLUE);
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
}
for (int i = 0; i < gridButton.length; i++) {
gridButton[i].setBackground(Color.WHITE);
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
}
}
for (int i = 0; i < gridButton.length; i++) {
gridButton[i].setEnabled(true);
gridButtonOwner[i] = 0;
}
}
};
Thread winnerRed = new Thread() {
public void run() {
for (int a = 0; a < 4; a++) {
for (int i = 0; i < gridButton.length; i++) {
gridButton[i].setBackground(Color.RED);
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
}
for (int i = 0; i < gridButton.length; i++) {
gridButton[i].setBackground(Color.WHITE);
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
}
}
for (int i = 0; i < gridButton.length; i++) {
gridButton[i].setEnabled(true);
gridButtonOwner[i] = 0;
}
}
};
public void windowBirth() {
for (int i = 0; i < gridButton.length; i++) {
gridButtonOwner[i] = 0;
gridButton[i] = new JButton("");
gridButton[i].addActionListener(this);
gridButton[i].setBackground(Color.WHITE);
grid.add(gridButton[i]);
}
windowTicTacToe.setDefaultCloseOperation(3);
windowTicTacToe.setLocation(400, 200);
windowTicTacToe.setPreferredSize(new Dimension(400, 400));
windowTicTacToe.add(grid);
windowTicTacToe.pack();
windowTicTacToe.setVisible(true);
}
public void actionPerformed(ActionEvent gridButtonClicked) {
for (int i = 0; i < gridButton.length; i++) {
if (gridButtonClicked.getSource() == gridButton[i]) {
if (turn == 1) {
turn = 2;
gridButtonOwner[i] = 1;
gridButton[i].setBackground(Color.blue);
gridButton[i].setEnabled(false);
} else {
turn = 1;
gridButtonOwner[i] = 2;
gridButton[i].setBackground(Color.red);
gridButton[i].setEnabled(false);
}
}
}
checkWinner();
}
public void checkWinner() {
for (int a = 1; a < 3; a++) {
HolerDia1 = a;
HolerDia2 = a;
for (int b = 0; b < gridSize; b++) {
HolerHor = a;
HolerVer = a;
for (int c2 = 0; c2 < gridSize; c2++) {
HolerHor = (HolerHor * gridButtonOwner[((b * gridSize) + c2)])/ a;
HolerVer = (HolerVer * gridButtonOwner[((c2 * gridSize) + b)])/ a;
}
if (HolerHor == a || HolerVer == a) {
winnerAnimation(a);
}
}
for(int h = 0;h < gridSize; h++){
HolerDia1 = (HolerDia1 * gridButtonOwner[h * (gridSize + 1)]) / a;
HolerDia2 = (HolerDia2 * gridButtonOwner[(h * (gridSize - 1)) + (gridSize - 1)]) / a;
}
if (HolerDia1 == a || HolerDia2 == a) {
winnerAnimation(a);
}
}
}
public void winnerAnimation(int b) {
for (int i = 0; i < gridButton.length; i++) {
gridButton[i].setEnabled(false);
}
if (b == 1){
winnerBlue.start();
}else{
winnerRed.start();
}
}
}
This is my question, When a player wins, for example Player 1 (Blue), it plays the animation (Flashing the board Blue). But when Player 1 wins again, the program crashes.
I looked into it a little and found that you can't start a thread twice because you just can't.
How would i go about pausing the thread, then restarting it when i need to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以简单地创建一个新线程并在需要时运行它。但不管怎样,我会使用 Swing 计时器来制作动画,因为它更简单、更安全,因为您不必担心因意外踩到 Swing 线程(EDT)而导致有害的间歇性崩溃。
例如,
You could simply create a new Thread and run it when needed. But regardless of this, I'd do the animation using a Swing Timer as it's simpler and safer since you don't have to worry about pernicious intermittent crashes from accidently stepping on the Swing thread, the EDT.
For e.g.,
在您的情况下,重播动画线程时,您可能需要:
可运行
。然后只需重新创建线程并启动它:
顺便说一句,在编写
Thread
或Runnable
时,您可能需要考虑 Swing 不是线程安全的事实。In your case, where the animation thread is replayed, you may want to:
Runnable
object, e.g. rather than creating an anonymousThread
object, create an anonymousRunnable
.Then simply recreate the thread and start it:
BTW, you may want to consider the fact that Swing is not thread-safe when writing your
Thread
s orRunnable
s.使用信号量。
Use Semaphores.
创建另一个线程实例有什么问题?重用线程是令人讨厌的,可能会导致非常奇怪的行为,通常不应该这样做。
除了重复代码(线程中只有 1 行差异,对吗?)之外,您可以简单地将 Thread 实现为内部类。这样您就不必使用匿名实例,并且可以根据需要多次重新创建和启动。
您绝对应该考虑将颜色作为参数添加到该线程中!
(我很确定这里存在编译器错误,但您应该明白我的意思。)
What's wrong with creating another instance of the Thread? Reusing Threads is nasty, can lead to really weird behaviour and generally shouldn't be done.
Besides the repetition of your code (There is only 1 line difference in the Threads, right?), you can simply implement the Thread as an inner class. This way you don't have to use an anonymous instance and you can recreate and start is as many times as you want.
You should definetly think about adding the color as a parameter to this Thread!
(I'm pretty sure there's a compiler error in here, but you should get what I mean.)
看一下 java 的
wait()
和notify()
方法。 这里是关于它们的教程。Have a look at java's
wait()
andnotify()
methods. Here is a tutorial on them.可以使用以下函数暂停和恢复线程:
实现:
但我认为这些功能已被弃用。但不确定。
Threads can be paused and resumed using the following functions :
Implementation:
But i think these functions are deprecated. Not sure though.