I can't understand the following statement

The Hotspot VM now implements Thread.yield() using the Windows SwitchToThread() API call. This call makes the current thread give up its current timeslice, but not its entire quantum.

given at http://www.javamex.com/tutorials/threads/yield.shtml

I understand that after a thread yields, other threads get a chance to execute. The original thread finishes its time slice and goes back to the queue. What's quantum here ?

As far as I can make out, quantum is the total time that a process has to spend in order to complete. Time slices are the time intervals that each process is given, before its shifted to the end of the queue and other processes are given chance to execute.

As far as I can make out, quantum is the total time that a process has to spend in order to complete

No, read the article again.

Each thread has a quantum, which is effectively how long it is allowed to keep hold of the CPU
[...]
a quantum is usually a small number of clock ticks, depending on the OS
[...]
a clock tick is typically 10-15 ms under Windows; under Linux, it is 1ms

BTW, yield is not about processes; it's about threads.

Edited 5 Years Ago by ~s.o.s~: n/a

I think that processes as well as threads are given time slices. Within the time slice, each process or thread has a specified quantum that it executes and then other threads execute their time quantums within the same time slice. I read it here : http://www.fcolakoglu.com/lottery_scheduling.html.

every process has a specified time quantum whose duration is computed when the time slice begins. In general, different processes have different time quantum durations. The time quantum value is the maximum CPU time portion assigned to the process in that time slice. When a process has exhausted its time quantum, it is preempted and replaced by another runnable process. Of course, a process can be selected several times from the scheduler in the same time slice, as long as its quantum has not been exhausted

Sorry, I am totally confused as to the difference between a time slice and a quantum here. In most literature, they are synonymous.

Edited 5 Years Ago by daudiam: n/a

Windows
In the Hotspot implementation, the way that Thread.yield() works has changed between Java 5 and Java 6.

In Java 5, Thread.yield() calls the Windows API call Sleep(0). This has the special effect of clearing the current thread's quantum and putting it to the end of the queue for its priority level. In other words, all runnable threads of the same priority (and those of greater priority) will get a chance to run before the yielded thread is next given CPU time. When it is eventually re-scheduled, it will come back with a full full quantum, but doesn't "carry over" any of the remaining quantum from the time of yielding. This behaviour is a little different from a non-zero sleep where the sleeping thread generally loses 1 quantum value (in effect, 1/3 of a 10 or 15ms tick).

In Java 6, this behaviour was changed. The Hotspot VM now implements Thread.yield() using the Windows SwitchToThread() API call. This call makes the current thread give up its current timeslice, but not its entire quantum. This means that depending on the priorities of other threads, the yielding thread can be scheduled back in one interrupt period later. (See the section on thread scheduling for more information on timeslices.)

Linux
Under Linux, Hotspot simply calls sched_yield(). The consequences of this call are a little different, and possibly more severe than under Windows:

a yielded thread will not get another slice of CPU until all other threads have had a slice of CPU;
(at least in kernel 2.6.8 onwards), the fact that the thread has yielded is implicitly taken into account by the scheduler's heuristics on its recent CPU allocation— thus, implicitly, a thread that has yielded could be given more CPU when scheduled in the future.

Right. But still what is the difference between time slice and quantum ? I am not able to understand. They are synonymous in most literature (even in Wikipedia). Why is a time slice allocated to processes when other processes can share a quantum in even that ?

I think that processes as well as threads are given time slices

Yes, but "Thread.yield", as mentioned in your original post specifically refers to threads.

But still what is the difference between time slice and quantum ? I am not able to understand. They are synonymous in most literature (even in Wikipedia)

The terms are indeed used synonymously all over the internet. There was one message on some micro-kernel mailing list which jots down the difference (which again isn't very clear):

In short, the timeslice determines for how long a thread can
continuously occupy a CPU before being preempted, the quantum limits the
total time the thread may spend on the CPUs accumulated over all
occasions the thread was scheduled.

Maybe you should mail Neil (the original author of the article) or PM neilcoffey (the author of the article) on Daniweb for clarifications.

This article has been dead for over six months. Start a new discussion instead.