如何暂停/恢复 Java 线程

发布于 2024-10-31 03:14:31 字数 5216 浏览 0 评论 0原文

我正在用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

聽兲甴掵 2024-11-07 03:14:31

您可以简单地创建一个新线程并在需要时运行它。但不管怎样,我会使用 Swing 计时器来制作动画,因为它更简单、更安全,因为您不必担心因意外踩到 Swing 线程(EDT)而导致有害的间歇性崩溃。

例如,

   public void myWinner(final Color flashColor) {
      int timerDelay = 300;
      new javax.swing.Timer(timerDelay , new ActionListener() {
         private static final int COUNTER_MAX = 5;
         int counter = 0;

         public void actionPerformed(ActionEvent e) {
            if (counter >= COUNTER_MAX) { // time to stop
               ((Timer)e.getSource()).stop();
               for (int i = 0; i < gridButton.length; i++) {
                  gridButton[i].setBackground(Color.white); // just to be sure
                  gridButton[i].setEnabled(true);
                  gridButtonOwner[i] = 0;
               }
            }
            Color bckgrndColor = (counter % 2 == 0) ? flashColor : Color.white;
            for (JButton button : gridButton) {
               button.setBackground(bckgrndColor);
            }
            counter++;
         }
      }).start();
   }

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.,

   public void myWinner(final Color flashColor) {
      int timerDelay = 300;
      new javax.swing.Timer(timerDelay , new ActionListener() {
         private static final int COUNTER_MAX = 5;
         int counter = 0;

         public void actionPerformed(ActionEvent e) {
            if (counter >= COUNTER_MAX) { // time to stop
               ((Timer)e.getSource()).stop();
               for (int i = 0; i < gridButton.length; i++) {
                  gridButton[i].setBackground(Color.white); // just to be sure
                  gridButton[i].setEnabled(true);
                  gridButtonOwner[i] = 0;
               }
            }
            Color bckgrndColor = (counter % 2 == 0) ? flashColor : Color.white;
            for (JButton button : gridButton) {
               button.setBackground(bckgrndColor);
            }
            counter++;
         }
      }).start();
   }
生来就爱笑 2024-11-07 03:14:31

在您的情况下,重播动画线程时,您可能需要:

  • 将线程封装为其自己的 Runnable 对象,例如,不要创建匿名 Thread 对象,而是创建一个匿名可运行
  • 然后只需重新创建线程并启动它:

    public void WinnerAnimation(int b)
    {
        ...
        如果(b==1)
        {
            动画线程 = 新线程(winnerBlue);
            动画线程.start();
        }
        别的
        {
            动画线程 = 新线程(winnerRed);
            动画线程.start();
        }
    }
    

    顺便说一句,在编写 ThreadRunnable 时,您可能需要考虑 Swing 不是线程安全的事实。

In your case, where the animation thread is replayed, you may want to:

  • Encapsulate the thread as its own Runnable object, e.g. rather than creating an anonymous Thread object, create an anonymous Runnable.
  • Then simply recreate the thread and start it:

    public void winnerAnimation(int b)
    {
        ...
        if (b == 1)
        {
            animationThread = new Thread(winnerBlue);
            animationThread.start();
        }
        else
        {
            animationThread = new Thread(winnerRed);
            animationThread.start();
        }
    }
    

    BTW, you may want to consider the fact that Swing is not thread-safe when writing your Threads or Runnables.

荒路情人 2024-11-07 03:14:31

使用信号量

  1. 在程序开头启动线程。
  2. 每个 run() 方法应该连续循环。
  3. 在循环的顶部,acquire() 适当的信号量(红色/蓝色)。
  4. 当检测到获胜者时,release() 适当的信号量。

Use Semaphores.

  1. Start the threads at the beginning of the program.
  2. Each run() method should loop continuously.
  3. At the top of the loop, acquire() the appropriate semaphore (red/blue).
  4. When a winner is detected release() the appropriate semaphore.
活雷疯 2024-11-07 03:14:31

创建另一个线程实例有什么问题?重用线程是令人讨厌的,可能会导致非常奇怪的行为,通常不应该这样做。

除了重复代码(线程中只有 1 行差异,对吗?)之外,您可以简单地将 Thread 实现为内部类。这样您就不必使用匿名实例,并且可以根据需要多次重新创建和启动。
您绝对应该考虑将颜色作为参数添加到该线程中!

(我很确定这里存在编译器错误,但您应该明白我的意思。)

// other stuff here...

public void winnerAnimation(int b) {
    for (int i = 0; i < gridButton.length; i++) {
        gridButton[i].setEnabled(false);
    }
    if (b == 1){
        new WinnerThread(Color.BLUE).start();
    }else{
        new WinnerThread(Color.RED).start();
    }
}

class WinnerThread extends Thread {
   private Color color;
   public WinnerThread(Color c)
   {
       color = c;
   }

   public void run() {
            for (int a = 0; a < 4; a++) {
                for (int i = 0; i < gridButton.length; i++) {
                    gridButton[i].setBackground(color);
                }
                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;
            }
        }
    }
}

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.)

// other stuff here...

public void winnerAnimation(int b) {
    for (int i = 0; i < gridButton.length; i++) {
        gridButton[i].setEnabled(false);
    }
    if (b == 1){
        new WinnerThread(Color.BLUE).start();
    }else{
        new WinnerThread(Color.RED).start();
    }
}

class WinnerThread extends Thread {
   private Color color;
   public WinnerThread(Color c)
   {
       color = c;
   }

   public void run() {
            for (int a = 0; a < 4; a++) {
                for (int i = 0; i < gridButton.length; i++) {
                    gridButton[i].setBackground(color);
                }
                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;
            }
        }
    }
}
一抹淡然 2024-11-07 03:14:31

看一下 java 的 wait()notify() 方法。 这里是关于它们的教程。

Have a look at java's wait() and notify() methods. Here is a tutorial on them.

向地狱狂奔 2024-11-07 03:14:31

可以使用以下函数暂停和恢复线程:

  • suspend() = 用于暂停
  • resume() = 用于恢复

实现:

threadObject.挂起();

threadObject.恢复();

但我认为这些功能已被弃用。但不确定。

Threads can be paused and resumed using the following functions :

  • suspend() = for pausing
  • resume() = for resuming

Implementation:

threadObject.suspend();

threadObject.resume();

But i think these functions are deprecated. Not sure though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文