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 ...?
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.
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() .
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With