• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread priority for main and worker thread

 
Nancy Antony
Ranch Hand
Posts: 145
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

Look athe code below



when I execute, main exits message appears in between the worker thread output. As per my understanding, Java threading is pre-emptive. Main gets created with priority 5. Worker thread is created also with priority 5. Main sleeps, control passes to worker. As both are with same priority, when main() thread wakes up it will be put to runnable pool. As the worker thread is running with the same priority, worker thread should finish first and then main exits should be printed. But control passes to main and it prints main exits and then worker thread continues.

Any LIGHT on this unexpected behaviour. Code is executed in Linux as well as Windows XP.

Kindly explain.

Regards to all who have reply and also who do not have.

Nancy
 
Swastik Dey
Rancher
Posts: 1674
7
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have said Main sleeps, control passes to worker
. However as per your code t.start() is before Thread.sleep. So worker thread starts before sleep is called and goes to another pool. Sleep gets over and main terminates however the worker thread is still running. If you change the condition in for loop to for e.g. i<=1 you are likely to get main exists as the first message because in that case the worker thread completes its execution even before sleep gets over.

Correct me if I am wrong.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For starters, this can easily be implementation-dependent. I just ran the code a few times, and mostly the "Main exits" message was the last to be printed, but a couple of times in between the others.

As the worker thread is running with the same priority, worker thread should finish first

No. Both threads have the same priority, so it's up to the JVM to decide which one to run. Pre-emptive means that thread execution can be put on hold in favor of running some other thread, and then resumed later.

What's more, calling t.start() does not guarantee that the thread starts executing right then. It very likely starts no later than the sleep method call, but the JVM might decide that 10ns is so short that swapping in a different thread is not worth the effort, and continues running the main thread for some more time. Or maybe it calls the GC during that time.

Plus, the JVM may or may not run threads on different CPU cores, so there may be actual parallelism at work (or not, you can't be sure).

And lastly, even though it plays no role here: Thread priorities are not something one should put a lot of stock in. In particular, trying to control which threads are run by giving them different priorities is bound not to work well. It seems like it should, but it just doesn't.
 
Nancy Antony
Ranch Hand
Posts: 145
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when I executed is it display worker thread msg for about 10 times. And then main exits was printed which shows that control transfer happened to worker thread. Usually pre-emptive model say until high-priority thread pre-empts, current thread keeps running or to transfer control b/w same priority threads I should use yield(0. As I'm not using yield() so I expect main exits to be printed after end of worker thread, as Java says when thread wakes up it goes to runnable pool and does not pre-empt same priority thread.

What do you say?

Regards,
Nancy
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nancy Antony wrote:What happened when I executed is it display worker thread msg for about 10 times. And then main exits was printed which shows that control transfer happened to worker thread. Usually pre-emptive model say until high-priority thread pre-empts, current thread keeps running or to transfer control b/w same priority threads I should use yield(0. As I'm not using yield() so I expect main exits to be printed after end of worker thread, as Java says when thread wakes up it goes to runnable pool and does not pre-empt same priority thread.


What you're describing is not preemptive multitasking; it's the old-fashioned so-called "cooperative multitasking" model, wherein a thread will continue to run until it specifically does something to give up the CPU. The very earliest JVMs worked that way, over 15 years ago. Current JVMs universally use time-slicing, in which high-priority threads may get a higher percentage of CPU time, but every runnable thread, regardless of priority, will get a share of the available time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!