• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads - priority

 
bolisetti sriharsha
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning threads and got stuck here

The output I am getting is
5
5
10
This is from main
This is a new thread!

My question is Shouldn't MyThread be executed first as it has highest priority ?

Please Explain

Thanks in advance
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling Thread.start does not mean that the thread will start executing right at that moment; the JVM must first decide to switch away from the current thread. I'm not sure what all the factors are that go into that decision, but the length of the remaining byte code (very short in this case) may well be a factor.

In general, you should not rely on setting thread priorities for trying to force scheduling decisions; that may not work the way you expect, or not work at all.
 
Henry Wong
author
Marshal
Pie
Posts: 20826
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bolisetti sriharsha wrote:I am learning threads and got stuck here

The output I am getting is
5
5
10
This is from main
This is a new thread!

My question is Shouldn't MyThread be executed first as it has highest priority ?

Please Explain

Thanks in advance



Hint: What happens when the OS has to choose between a high priority thread that isn't runnable (or started yet) and a lower priority thread that is runnable?

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic