Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What makes the execution order of threads unpredictable?

What makes the execution order of threads unpredictable? Does the scheduler at some point use random numbers or check system resources or see which thread has waited long enough or ...?

like image 841
Rnet Avatar asked Oct 27 '11 11:10

Rnet


People also ask

Why is thread behavior described as unpredictable?

Answer : Thread behaviour is unpredictable because execution of Threads depends on Thread scheduler, thread scheduler may have different implementation on different platforms like windows, unix etc. Same threading program may produce different output in subsequent executions even on same platform.

Can we decide order of execution of threads?

You cannot tell the thread scheduler which order to execute threads in. If you need to ensure that a certain piece of code which is running on thread A must run before another piece of code running on thread B, you must enforce that order using locks or wait() / notify() .

How do you control order of thread execution?

Executing threads in Sequence in Java It can be seen that threads are executed in sequence here. Thing to do here is you start the thread and call the join() method on the same thread. This makes it to wait until the thread stops executing. That way order is ensured.

Does execution of threads depend on the thread scheduler?

Even on modern systems that have many CPU cores and thus can be concurrently executing multiple threads, they are likely no match for the number of threads in a runnable state on a system.


3 Answers

The scheduler is, usually, the OS's scheduler. It is influenced by many factors, including what other processes on the machine are doing, what the hardware is doing (interrupts), etc. Depending on the OS, I suppose there may sometimes be random numbers involved, but I suspect generally not. It's more just the unpredictable way that multiple variable time intervals can overlap.

like image 107
Ernest Friedman-Hill Avatar answered Oct 19 '22 07:10

Ernest Friedman-Hill


Using random numbers in the scheduler would introduce unnecessary overhead into a critical section of the OS, so it's very unlikely that is the cause, at least in any mainstream OS.

A thread usually runs until it makes an OS call that would block, or until an interrupt occurs, or until its time slice expires (which ultimately is just a timer interrupt). Even if you could carefully construct things so that two threads would always block in a deterministic order, you have no control over precisely when the latter two effects will occur. The order the threads in your application are executed in will eventually be influenced by events outside your application.

like image 29
JustJeff Avatar answered Oct 19 '22 06:10

JustJeff


Other questions make good points with technical details, but:

To be precise, thread scheduling in Java is fairly efficiently controlled by locks, wait/notify/notifyAll, sleep methods and other concurrency controls. Only at those times during application execution, when these are not present, the execution order of different threads is left undefined.

Main reason is probably for the sake of ease of portability of Java in different hardware/OS systems. It is also only logical, that if you as a developer do not define order in which different threads in your app should be executed using above mentioned concurrency controls, you do not care about it and it just does not matter then and arbitrary way may be chosen by the JVM.

like image 36
MarianP Avatar answered Oct 19 '22 07:10

MarianP