哪个Thread.sleep命令会暂停哪些线程?

huangapple go评论78阅读模式
英文:

Which Thread.sleep commands pauses which threads?

问题

我有四个关于 Thread.sleep(...) 的声明如下。每个声明都标有 Line #1 到 #6。我的问题是哪些声明会让哪些线程暂停?

class Runa extends Thread{
    public void run(){
        try{
            // Line #1
            Thread.sleep(500);
            //Line #2
            this.sleep(500);
        }catch(Exception e) {}
    }
}

class Runb implements Runnable {
    Thread t;

    Runb() {
        t = new Thread(this);
        t.start();

        try{
            //Line #3
            Thread.sleep(500);

        }catch(Exception e){ }
    }

    @Override
    public void run() {
     
        try {
            do {

                // Line #4
                Thread.sleep(2000);
                // Line #5
                // t.sleep(500);
                count++;
            } while (count < 10);
        } catch (InterruptedException e) {

        }

    }
}

public class thread2Runnable2 {
    public static void main(String args[]) {          
        Runa rua = new Runa();
        rua.start();
        //Line #6
        rua.sleep(500); 
       
        Runb runb = new Runb();    
    }
}

以下是我的假设:

Line #1 暂停 Runa 线程
Line #2 暂停 Runa 线程
Line #3 暂停主线程
Line #4 暂停 t 线程
Line #5 暂停 t 线程
Line #6 暂停主线程

我的假设正确吗?

英文:

I have four declarations of Thread.sleep(...) below. Each of the declarations is marked with that Line #1 to #6. My question is which declarations put which threads to pause?

class Runa extends Thread{
    public void run(){
        try{
            // Line #1
            Thread.sleep(500);
            //Line #2
            this.sleep(500);
        }catch(Exception e) {}
    }
}

class Runb implements Runnable {
    Thread t;

    Runb() {
        t = new Thread(this);
        t.start();

        try{
            //Line #3
            Thread.sleep(500);

        }catch(Exception e){ }
    }

    @Override
    public void run() {
     
        try {
            do {

                // Line #4
                Thread.sleep(2000);
                // Line #5
                // t.sleep(500);
                count++;
            } while (count &lt; 10);
        } catch (InterruptedException e) {

        }

    }
}

public class thread2Runnable2 {
    public static void main(String args[]) {          
        Runa rua = new Runa();
        rua.start();
        //Line #6
        rua.sleep(500); 
       
        Runb runb = new Runb();    
    }
}

These are my assumptions:

Line #1 pause Runa thread
Line #2 pause Runa thread
Line #3 pause the main thread
Line #4 pause t thread
Line #5 pause t thread
Line #6 pause the main thread

Are my assumptions correct?

答案1

得分: 6

Thread#sleep(long)方法是一个_静态_方法,它:

>使当前正在执行的线程休眠(暂时停止执行)指定的毫秒数,受系统计时器和调度程序的精度和准确性的影响。

当前正在执行的线程是调用该方法的线程。因此,无论哪个线程调用sleep,就是哪个线程在休眠。据我所知,您的假设似乎是正确的。

英文:

The Thread#sleep(long) method is a static method which:

>Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.

The currently executing thread is the thread which invoked the method. So whichever thread invokes sleep is the one which sleeps. As far as I can tell, your assumptions appear correct.

答案2

得分: 5

Thread.sleep()是一个静态方法,因此调用它不依赖于你在哪个对象上调用:它总是作用于当前正在执行的线程。

事实上,你可以在实例(例如 runa)上以语法方式调用该方法,这是Java的一个怪异之处,可能会非常令人困惑。许多集成开发环境(IDE)和其他代码质量工具会将这种调用标记为警告,因为对象引用对调用完全无关,因此可能会误导阅读代码的人。

如果你使用正常的静态调用语法,Thread.sleep(NNNN),你会注意到没有传递对象引用,即使不阅读文档,也很容易得出唯一合理的结论,即在当前线程上执行。

英文:

Thread.sleep() is a static method, so it doesn't matter which object you call it on: it will always act on the currently executing thread.

The fact that you can call this method on an instance (e.g. runa) syntax-wise at all is a quirk of Java and can be very confusing. Many IDEs and other code-quality tools will highlight such calls as warnings as the object reference is completely irrelevant to the call and thus can be misleading to someone reading the code.

If you use the normal static call syntax, Thread.sleep(NNNN), you see that no object reference is passed, which, even without reading the documentation, easily leads to the conclusion that the only reasonable behavior is to act on current thread.

huangapple
  • 本文由 发表于 2020年7月24日 14:24:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63068005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定