对单个线程使用 sleep() [英] using sleep() for a single thread

查看:31
本文介绍了对单个线程使用 sleep()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Java 还很陌生,我开始使用不同的线程以便在我的代码的一部分上使用 wait()sleep()并让其他人继续运行.

I am fairly new to java, and am starting to get into using different threads in order to use wait() or sleep() on one part of my code and have the others still run.

对于这个项目,我将 JFramejavax.swing.*java.awt.* 导入一起使用.我想要做的是让其中一个线程(在我的代码中它是主要的起始线程)允许玩家在井字棋盘上选择一个空间,当他们点击它时,它会改变图标,并且然后 AI 将等待 1 秒,然后从我创建的第二个线程播放.

For this project, I am using JFrame with the javax.swing.* and java.awt.* imports. What I am trying to do is have one of the threads (in my code it is the main, starting thread) allow the player to choose a space on the tic tac toe board, and when they click it, it will change icons, and then the AI will wait for 1 second before playing back from the second thread that I created.

不幸的是,每当我调用 ait.sleep(1000)(ait 是我的线程名称)时,两个线程在完成执行之前都会等待 1 秒.谁能告诉我为什么睡眠一个线程会阻止我的整个执行?

Unfortunately, whenever I call ait.sleep(1000) (ait is my thread name) both threads wait for 1 second before finishing their execution. Can anyone tell me why sleeping one thread is stopping my whole execution?

推荐答案

谁能告诉我为什么睡一个线程会阻止我的整个过程执行

Can anyone tell me why sleeping one thread is stopping my whole execution

为了更好地解释你的 Swing GUI 是在它自己的特殊线程上创建的,与 main() 和其他代码将在其中运行的线程分开,这是通过创建你的SwingUtilities.invokeXXX 块中的 Swing 组件(即使您没有这样做,您的 GUI 也将在名为 初始线程) .现在,如果您只是在 Event Dispatch Thread 上调用 sleep(或者在同一个 Thread 上),它将等待对 sleep 的调用code>Thread.sleep 完成.现在因为所有 Swing 事件都是在 EDT 上处理的,我们通过调用 sleep(..) 暂停它的执行,从而暂停处理 UI 事件,因此 GUI 被冻结(直到 sleep(..) 返回).

to better explain your Swing GUI is created on its own special thread separate from that which main() and other code will run in, this is done via creating your Swing components in the SwingUtilities.invokeXXX block (even if you have not done this your GUI will be run on a single thread called the initial thread) . Now if you simply call sleep while on Event Dispatch Thread (or for that matter on the same Thread) it will wait for the call to Thread.sleep to finish. Now because all Swing events are processed on EDT we pause its execution by calling sleep(..) thus pausing the UI events from being processed and therefore GUI is frozen (until sleep(..) returns).

您不应在 上使用 Thread.sleep(..)Event Dispatch Thread(或任何 Thread,其中 sleep 会导致不必要的执行阻塞),因为这会导致 UI 看起来被冻结.

You should not use Thread.sleep(..) on Event Dispatch Thread (or any Thread where sleep will cuase unwanted execution blocking), as this will cause the UI to seem frozen.

这里是一个很好的例子,它准确地演示了这种由调用 Thread.sleep 引起的不良行为(..) 在 GUI 的 EDT 上.

Here is a nice example which demonstrates exactly, this unwanted behavior caused by invoking Thread.sleep(..) on GUI's EDT.

宁愿使用:

int delay=1000;// wait for second

Timer timer = new Timer(delay, new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent ae) {
        //action that you want performed 
    }
});
//timer.setRepeats(false);//the timer should only go off once
timer.start();

  • Swing Worker

    或者如果没有创建/修改 Swing 组件:

    or if no Swing components are being created/modified:

    线程,然后您将使用 Thread.sleep(int milis)(但无论如何这是 IMO 的最后一个选项)

    Thread, you would then use Thread.sleep(int milis) (but thats last option in any case IMO)

    更新

    Swing Timer/SwingWorker 只在 Java 1.6 中被添加,然而,TimerTaskThread 已经存在对于更长时间的正弦 Java 1.3 和 JDK 1,您甚至可以使用上述两种方法中的任何一种,并将在 SwingUtilities/EventQueue#invokeXX 块中创建/操作 Swing 组件的调用包装起来;这就是过去做事的方式:P

    Swing Timer/SwingWorker was only added in Java 1.6, however, TimerTask and Thread have been around for alot longer sine Java 1.3 and JDK 1 repsectively, thus you could even use either of the 2 above methods and wrap calls that create/manipulate Swing components in SwingUtilities/EventQueue#invokeXX block; thats the way things used to be done :P

    这篇关于对单个线程使用 sleep()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆