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

查看:127
本文介绍了对单个线程使用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.

对于这个项目,我使用 JFrame 使用 javax.swing。* java.awt。* 导入。我想要做的是让其中一个线程(在我的代码中它是主要的,起始线程)允许玩家在tic tac toe board上选择一个空间,当他们点击它时,它将改变图标,并且然后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中创建Swing组件来完成的.invokeXXX 阻止(即使你没有这样做,你的GUI将在一个名为初始线程)。现在,如果你只是在事件调度线程上调用 sleep (或者在同一个线程)它将等待 Thread.sleep 的调用完成。现在因为在EDT上处理所有Swing事件,我们通过调用 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(.. ) 事件派遣线程 $ c> (或任何线程,其中睡眠将阻止不必要的执行阻止),因为这将导致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.

这里是一个很好的例子,它完全展示了通过在GUI的EDT上调用 Thread.sleep(..)引起的这种不必要的行为。

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组件创建/修改:

    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中添加,但 TimerTask 线程已经存在了很长的正弦Java 1.3和JDK 1,因此您甚至可以使用上述两种方法中的任何一种并包装创建的调用/操作中的Swing组件SwingUtilities / EventQueue#invokeXX 块;这就是过去的方式: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天全站免登陆