aspose file tools*
The moose likes Threads and Synchronization and the fly likes Thread Scheduling - reg. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Scheduling - reg." Watch "Thread Scheduling - reg." New topic
Author

Thread Scheduling - reg.

dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
Hi All,

I have a very basic doubt. Consider the following scenario:

1) I am creating three instances of a class that implements Runnable. (t1,t2,t3)

2) Then i am setting the priorty levels of the threads MIN_PRIORITY,NORM_PRIORIY,MAX_PRIORITY respectively for t1, t2, t3.

3) Now i am invoking the start() method on each objects first t1.start(), then t2.start() and t3.start().

4) The Scheduler first handles the control to t3 thread's run method since it is having the maximum priority.

5) I am running this application in Windows 2000 operated PC. I am wondering how the OS scheduling mechanism is controlled by an application running on top of it since Windows2000 uses <b>RoundRobin</b> Scheduling for scheduling the processes.

Sorry if any of my assumptions in this question is wrong.

Thanks in Advance!!!
dinesh.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
There a few guarantees about thread scheduling in Java and you shouldn't write Java applications in a way that requires very tightly-controlled thread scheduling. For most applications, you really don't need them if you design them right. For those where you really do need them, Java may not be the best choice of language.

Thread priorities are a suggestion to the Java system that it might like to give more priority or less priority to certain threads. There is no guarantee that it will do so.

In practice, on Windows, OS X and Linux, I find that it does actually respect your suggested priorities pretty well. JVMs typically just map the Java priority value to an OS-specific priority value, then pass it to the OS thread scheduling functions.

I believe Windows 2000 and all relatively recent Windows versions have a fairly sophisticated thread scheduling scheme. It may be based on the "round robin" idea, but I'm pretty sure it's smarter than that. M$ probably document their scheme to some extent: Google would be your friend here.
[ January 08, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40

Thread priorities are a suggestion to the Java system that it might like to give more priority or less priority to certain threads. There is no guarantee that it will do so.


Yup. All the JVM does is pass that priority to the underlying threading system (with some modification as Windows has 2 less priority levels). It is up to Windows to decide how to schedule it.

As for what Windows does, this priority is further mapped to an effective priority, which is based on a complex formula of the original priority, when was the last time the thread ran, the size of the last time-slice, etc. The purpose of this is to prevent thread starvation.

Basically, in Windows, higher priority threads run more often and for longer periods.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Alik Elzin
Greenhorn

Joined: Sep 19, 2002
Posts: 15
Hi,

Threads in Java and OS processes are 2 different things.
One should not even look @ Java threads as OS threads.
Look @ Java threads as internal threads handled by a single Java process - the JVM.
This way platform dependency is taken out - one of Java objectives.

Taken from wikipedia: http://en.wikipedia.org/wiki/Round-robin_scheduling:
"Round-robin is one of the simplest scheduling algorithms for processes in an operating system, which assigns time slices to each process in equal portions and in order, handling all processes without priority"

Java threads have priorities thus executed with consideration to them and not in round robin way.

I think it is wrong to count on thread scheduling for flow correctness and rather use synchronization and locking.

Regards,
Alik.
[ January 09, 2007: Message edited by: Alik Elzin ]
victor kamat
Ranch Hand

Joined: Jan 10, 2007
Posts: 247
One suggestion I have is to assign one Max priority and the other min priority. I DO regard this as very inelegant but came to mind right away.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Scheduling - reg.