How Thread.sleep () works when called from multiple threads

sleep () is a static method of the Thread class. How it works when calling from multiple threads. and how it determines the current thread of execution.

or maybe a more general question. How are static methods called from different threads? Will there be problems with concurrency?

+7
java multithreading synchronization static
source share
5 answers

How to determine the current flow of execution?

It's not obligatory. It simply calls the operating system, which always sleeps the thread that caused it.

+6
source share

The sleep method delays the current thread, so if you call it from multiple threads, it will sleep each of these threads. There is also a static currentThread method that allows you to get the current executable thread.

+5
source share

more general question: How do static methods call from different threads? Will there be problems with concurrency?

There is only a potential concurrency problem if one or more threads change the general state while another thread uses the same state. There is no general state for the sleep () method.

0
source share

Thread.sleep(long) is implemented initially in the java.lang.Thread class. Here is part of his API document:

  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 flushes the thread that calls it. (Based on EJP comments) defines the current executable thread (which called it and made it sleep). Java methods can determine which thread runs this by calling Thread.currentThread()

Methods (static or non-static) can be called from any number of threads at the same time. Threre there will be no concurrency problems if your methods are thread safe . You will only have problems if multiple threads change the internal state of a class or instance without proper synchronization.

-one
source share

When a virtual machine encounters sleep(long) -statement, it interrupts the current thread. The "current thread" at this point is always a thread called Thread.sleep() . Then he says:

Hey! There is nothing to do in this thread (because I have to wait). I will continue another topic.

Changing the flow is called "to yield". (Note: you can do it yourself by calling Thread.yield(); )

So, no need to figure out what the current thread is. This is always a Thread called sleep (). Note. You can get the current thread by calling Thread.currentThread();

A brief 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:

 // 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; 
-one
source share

All Articles