如何让另一个线程在Java中休眠 [英] How to make another thread sleep in Java

查看:128
本文介绍了如何让另一个线程在Java中休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展Thread的类。这个线程在运行时花费大部分时间睡觉,它将执行检查,如果为真执行简单操作,则休眠1/2秒并重复。

I have a class that extends Thread. This thread when running spends most of it's time sleeping, it will perform a check, if true perform a simple action, then sleep for 1/2 second and repeat.

class还有一个由其他线程调用的公共方法。如果调用它,我希望线程在睡眠时睡眠时间更长或者如果不是则立即睡觉。我试图让这个.sleep,但似乎这仍然睡在当前线程,它抱怨方法睡眠是静态的,应该静态访问。

The class also has a public method that is called by other threads. If this is called I want the thread to sleep for longer if it is already sleeping or just sleep immediately if it isn't. I tried to have this.sleep but it seems that this still sleeps the current thread and it complains that the method sleep is static and should be accesses statically.

这个程序显示我的问题,当调用CauseSleep时我想让它停止打印数字,直到睡眠结束。

This program shows my problem, when CauseSleep is called I want it to stop printing numbers until that sleep has finished.

public class Sleeper {
    public static void main(String[] args) {
        new Sleeper();
    }
    public Sleeper() {
        System.out.println("Creating T");
        T t = new T();
        System.out.println("Causing sleep");
        t.CauseSleep();
        System.out.println("Sleep caused");
    }
    public class T extends Thread {
        public T() {
            this.start();
        }
        public void run() {
            for (int i = 0; i < 30; i++) {
                System.out.println("T Thread: " + i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
            }
        }
        public void CauseSleep() {
            try {
                this.sleep(2000);
            } catch (InterruptedException e) {
            }
        }
    }
}

我得到的输出是

Creating T
Causing sleep
T Thread: 0
T Thread: 1
T Thread: 2
T Thread: 3
T Thread: 4
T Thread: 5
T Thread: 6
T Thread: 7
T Thread: 8
T Thread: 9
T Thread: 10
T Thread: 11
T Thread: 12
T Thread: 13
T Thread: 14
T Thread: 15
T Thread: 16
T Thread: 17
T Thread: 18
Sleep caused
T Thread: 19
T Thread: 20
T Thread: 21
T Thread: 22
T Thread: 23
T Thread: 24
T Thread: 25
T Thread: 26
T Thread: 27
T Thread: 28
T Thread: 29


推荐答案

你不能让另一个线程睡眠。 (您可以使用已弃用的 suspend()方法,但请不要这样做。此电话:

You can't make another thread sleep. (You can use the deprecated suspend() method, but please don't). This call:

this.sleep(200);

实际上会使当前正在执行的线程睡眠 - 而不是线程由this引用。 sleep 是一个静态方法 - 好的IDE会在该行上发出警告。

will actually make the currently executing thread sleep - not the Thread referred to by "this". sleep is a static method - good IDEs will issue a warning over that line.

你应该只有一面旗帜说请睡觉然后让睡眠者线程在做任何工作之前检查那个标志。

You should just have a flag saying "sleep please" and then make the sleeper thread check that flag before doing any work.

这是一个的东西你不能导致另一个线程入睡。假设它是在同步方法中 - 这意味着你在睡觉时持有一个锁,导致其他人试图获得相同的锁来阻止。不是一件好事。通过使用基于标志的系统,您可以以控制的方式进入睡眠状态 - 在您知道它不会造成伤害的位置。

It's a good thing that you can't cause another thread to sleep. Suppose it's in a synchronized method - that would mean you'd be holding a lock while sleeping, causing everyone else trying to acquire the same lock to block. Not a good thing. By using a flag-based system, you get to sleep in a controlled way - at a point where you know it's going to do no harm.

这篇关于如何让另一个线程在Java中休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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