views:

466

answers:

6

In Java, I know that the default thread priority is 5. I have a class which basically sleeps for n number of seconds while doing other stuff on the side. If I create 20 threads in random order, each with that class as the target with n being either 10 or 20 seconds, how is it that all the 10 second ones finish first, and then the 20 second ones finish.

What I thought would happen was that since I am running a dual core processor, only 2 threads can run actually simultaneously. Therefore, the scheduler would pick 2 threads arbitrarily, since they are all the same priority, and run them first, which means that sometimes a 20 second thread will have to run before a 10 second one and so on and so forth.

+6  A: 

I would assume this is because the quanta for the threads is much smaller than their run time of 10 or 20 seconds when they are scheduled. Even if a 20s thread gets into the run queue first, it will not actually run to completion all at once. It will get bumped back out to the wait queue by the thread scheduler after its time slice runs out, which is probably a comparatively short amount of time.

See here for a pretty good article on thread scheduling algorithms. The basic summary is that each thread will run for some small amount of time (on the order of milliseconds perhaps), then it will get swapped out for another thread. This thread swapping will continue until all threads are finished.

Additionally, the Wikipedia entry on scheduling is pretty good. Although some of it specifically refers to process scheduling, modern OSes generally deal with threads in the same manner.

eldarerathis
+2  A: 

All the threads are run "simultaneously". That's how threads work. Two threads run at the same time, but then get switched out so other threads can run (and so on...). Threads don't hold the processor until they are finished.

So it makes sense that the 10 second threads finish first.

Starkey
A: 

Its totally up to the jvm to determine which threads to run tun first. Even when the 20s thread is run first and put to sleep, a 10s thread can be started. So if the actual computation is small e.g. few milliseconds, all 10s threads will finish first.

fastcodejava
+3  A: 

A thread is not actually consuming CPU time while it is sleeping. Thread.sleep(10000) simply means "yield execution to other threads now, and only reconsider this thread for scheduling again after at least 10 seconds have passed". So all threads can sleep simultaneously.

Wim Coenen
A: 

The thread priority just indicates to the JVM what your threads should be prioritized in. There is no hard guarantees, and as the JVM is just a process on the operating system, external processes may influence the amount of CPU cycles available to the JVM and hence how the processes are actually run.

You should generally instead consider looking at Executors which allows you to stuff a lot of Runnables/Callables in a bucket where work is taken from. The exact way work is taken depends on the Executor you choose for the purpose. Also, use Thread.yield() a lot for long running code chunks, as it allows the JVM to reschedule.

Thorbjørn Ravn Andersen
A: 

Depending on your use case, you might be able to get close to the behaviour you seem to expect by combining a ThreadPoolExecutor with a PriorityQueue.

The Executor can limit you to running 2 tasks simultaneously – so tasks which sleep will block the other tasks.

The PriorityQueue can be made to ensure that when a thread is free, the highest priority remaining task is the one that gets selected to run next.

Now, there might be a little fuss around start-up if the first task you kick off is low priority, since this scheme won't pre-empt running tasks. This may or may not matter to you.

Note that the Java 5 API for Exectutors made this arrangement almost impossible to get to work. You really need the Java 6 changes unless you enjoy getting your hands really dirty in the reflection API...

Bill Michell