File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes threads: time-sliced versus preemptive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "threads: time-sliced versus preemptive" Watch "threads: time-sliced versus preemptive" New topic
Author

threads: time-sliced versus preemptive

Carlo Smits
Greenhorn

Joined: Jul 09, 2001
Posts: 22
I am having problems understanding the difference between
- time-sliced threads versus preemptive threads, on the one hand, and
- native threads versus green threads, on the other hand
1. Is is true that native threads are always preemptive and never time-sliced?
2. Is is true that green threads are never preemptive and always time-sliced?
3. are cooperative/round-robin threads the same as time-sliced threads?
4. Why is it useful to use yield() in time-sliced threads? The whole idea of a time-sliced method is that it interrupts the currently running thread to let other threads run. (therefore you wouldn't need yield() to do this?)
I hope somebody can clear some of these doubts,

------------------
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by Carlo Smits:
I am having problems understanding the difference between
- time-sliced threads versus preemptive threads, on the one hand, and
- native threads versus green threads, on the other hand
1. Is is true that native threads are always preemptive and never time-sliced?
2. Is is true that green threads are never preemptive and always time-sliced?
3. are cooperative/round-robin threads the same as time-sliced threads?
4. Why is it useful to use yield() in time-sliced threads? The whole idea of a time-sliced method is that it interrupts the currently running thread to let other threads run. (therefore you wouldn't need yield() to do this?)
I hope somebody can clear some of these doubts,


Everyone gets the piece of the pie--- Time-sliced
High priority get to use the CPU - Pre-emptive
Now which one is better than one? I don't know
Many OS does it different, some are pure time-sliced, some are pure pre-emptive and some are combo of both..
I believe windows is Time-sliced/RoundRobin, and Solaris is pre-emptive...
yield(), is used in the combo(timesliced/pre-emptive) so that even though every thread gets to execute equally, there may be some thread that needs absolute high priority than the other one.
HIH
Happy Holidays
Ragu
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hello Carlo
time-sliced threads versus preemptive threads

I don't think they are opposite features.
The Java Tutorial mentions that some operating systems are time-slicing. In these o.s. the thread currently executing doesn't continues untill if finish its run method, but it is preempted by other threads with the same priority. In this way the time of CPU time is parted among all the threads with the current maximum priority. Otherwise all the threads with the current maximum priority will be executed, but in a secuential mode. In the Java Tutorial there is a nice example to see if your o.s haves time-slicing. I know W95 is time-sliced.
Some o.s. are also preempted. A thread can stop execution because another thread with a bigger priority has awakened. In these o.s a program that got blocked doesn't spoil the rest of the programs because the o.s can preempt the blocked program to recover the control. If the threads were cooperative the multitasking capacity of the o.s. is reached only because a thread releases the execution, so if it goes mad, the rest of the programs follow it. W95 is preempted while W3.1 had cooperative threads.
Native threads means that the JVM access the threads available from the o.s. This has the advantage that if more than one CPU are available several threads could be executing really simultaneusly. I think green threads means that the JVM is not able to access native threads, maybe they don't exist, so the proper JVM implementation provides some support for threads.
yield must be used to make certain that the thread is currently executing, the one that has the biggest priority (almost always), yields the execution to other threads with the same priority. In this way we have managed programatically the same effect than that in time-slicing o.s. A Java programmer should not suppose that her programs are always to be runned in a time-sliced o.s. so its better to use yield.
Anyone can provide more insight?


SCJP2. Please Indent your code using UBB Code
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Or was WNT preempted while W95 cooperative?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Just adding my $0.02 to what Jose wrote.
When using green threads, the OS doesn't know about threads at all. It's up to the JVM to handle all details of the threading API. The only thing the OS sees is that there is a single process (the JVM) and a single thread. A Java thread is then an abstraction within the JVM which has to take care of executing each threads within the program according to its scheduling policies.
You can find a good description of native and green threads in the book Java Threads by Scott Oaks and Henry Wong (O'Reilly and Associates)
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Carlo Smits
Greenhorn

Joined: Jul 09, 2001
Posts: 22
About o.s, I think Windows 3.11 and Windows 95 where both time-sliced/cooperative, whereas Solaris and Windows NT are pre-emptive and newer versions of Solaris support both cooperative and preemptive.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: threads: time-sliced versus preemptive
 
Similar Threads
Thread q from RHE book
Thread doubt ???
Threads in Java
Behavior of yield() in Threads
Please give me reply.