简体   繁体   English

从多个线程调用时Thread.sleep()如何工作

[英]How does Thread.sleep() work when called from multiple threads

sleep() is a static method of class Thread. sleep()是Thread类的静态方法。 How does it work when called from multiple threads. 从多个线程调用时,它是如何工作的。 and how does it figure out the current thread of execution. 以及如何找出当前的执行线程。 ?

or may be a more generic Question would be How are static methods called from different threads ? 或者可能是更通用的问题,将是如何从不同的线程调用静态方法? Won't there be any concurrency problems ? 不会有并发问题吗?

how does it figure out the current thread of execution? 如何找出当前的执行线程?

It doesn't have to. 不必。 It just calls the operating system, which always sleeps the thread that called it. 它仅调用操作系统,该操作系统始终使调用它的线程休眠。

The sleep method sleeps the current thread so if you are calling it from multiple threads it will sleep each of those threads. sleep方法会休眠当前线程,因此,如果您从多个线程中调用它,则会休眠每个线程。 Also there's the currentThread static method which allows you to get the current executing thread. 另外还有currentThread静态方法,该方法使您可以获取当前正在执行的线程。

a more generic Question would be How are static methods called from different threads ? 一个更通用的问题是如何从不同线程调用静态方法? Won't there be any concurrency problems ? 不会有并发问题吗?

There is only a potential concurrency problem if one or more thread modifies shared state while another thread uses the same state. 如果一个或多个线程修改共享状态而另一个线程使用相同状态,则仅存在潜在的并发问题。 There is no shared state for the sleep() method. sleep()方法没有共享状态。

Thread.sleep(long) is implemented natively in the java.lang.Thread class. Thread.sleep(long)java.lang.Thread类中本地实现。 Here's a part of its API doc: 这是其API文档的一部分:

 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 thread 
 does not lose ownership of any monitors.

The sleep method sleeps the thread that called it.(Based on EJP's comments) determines the currently executing thread (which called it and cause it to sleep). sleep方法使调用它的线程进入睡眠状态(基于EJP的注释) 确定当前正在执行的线程(调用它并使它进入睡眠状态)。 Java methods can determine which thread is executing it by calling Thread.currentThread() Java方法可以通过调用Thread.currentThread()确定哪个线程正在执行它。

Methods (static or non static) can be called from any number of threads simultaneously. 可以同时从任意数量的线程中调用方法(静态或非静态)。 Threre will not be any concurrency problems as long as your methods are thread safe . 只要您的方法是线程安全的,那么Threre就不会有任何并发​​问题。 You will have problems only when multiple Threads are modifying internal state of class or instance without proper synchronization. 仅当多个线程在没有适当同步的情况下修改类或实例的内部状态时,您才会遇到问题。

When the virtual machine encounters a sleep(long) -statement, it will interrupt the Thread currently running. 当虚拟机遇到sleep(long)语句时,它将中断当前正在运行的线程。 "The current Thread" on that moment is always the thread that called Thread.sleep() . 那一刻的“当前线程”始终是称为Thread.sleep()的线程。 Then it says: 然后它说:

Hey! 嘿! Nothing to do in this thread (Because I have to wait). 该线程中无事可做(因为我必须等待)。 I'm going to continue an other Thread. 我将继续另一个线程。

Changing thread is called "to yield". 更改线程称为“屈服”。 (Note: you can yield by yourself by calling Thread.yield(); ) (注意:您可以通过调用Thread.yield();来自己屈服)

So, it doesn't have to figure out what the current Thread is. 因此,不必弄清楚当前的线程是什么。 It is always the Thread that called sleep(). 总是线程调用sleep()。 Note: You can get the current thread by calling Thread.currentThread(); 注意:您可以通过调用Thread.currentThread();获得当前线程Thread.currentThread();

A short example: 一个简短的例子:

// here it is 0 millis
blahblah(); // do some stuff
// here it is 2 millis
new Thread(new MyRunnable()).start(); // We start an other thread
// here it is 2 millis
Thread.sleep(1000);
// here it is 1002 millis

MyRunnable its run() method: MyRunnablerun()方法:

// here it is 2 millis; because we got started at 2 millis
blahblah2(); // Do some other stuff
// here it is 25 millis;
Thread.sleep(300); // after calling this line the two threads are sleeping...
// here it is 325 millis;
... // some stuff
// here it is 328 millis;
return; // we are done;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM