1 | Threads are implemented by a scheduler in Java, which asks the local operating system to run threads in the "runnable" state. |
2 | Typically, the OS runs each thread in turn for a "time slice". However, some operating systems (early versions of Solaris, e.g.) run a thread to completion unless another thread of higher priority preempts the running thread. |
3 | Example of two threads running on one cpu: Each thread gives up execution voluntarily (by executing yield(), suspend(), etc.) or because its time slice has ended: |
4 | Thread A |
5 | Thread B |