• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Q

 
Eric Low
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys

The above will print:
Thread#2 is running
Thread#2 is running
Thread#1 is running
Thread#1 is running
Thread#1 is running
Thread#1 is running
Thread#1 is running
Thread#2 is running
Thread#2 is running
Thread#2 is running
Since on loop 2, I have set the Thread #2 priority to be the same with Thread #1, why is that it finish Thread #1 loop first, then it continue with the rest of the loop of Thread #2?
Frankly, sometimes I dun really understand the behaviour of a thread.
Anyway, going to take SCJP tomorrow. So, wish me luck!
 
Brian Glodde
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the JLS: 17.12 Threads
Every thread has a priority. When there is competition for processing resources, threads with higher priority are generally executed in preference to threads with lower priority. Such preference is not, however, a guarantee that the highest priority thread will always be running, and thread priorities cannot be used to reliably implement mutual exclusion.
Good luck on the test!
 
Eric Low
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I have a further doubt regarding the following code:

which always prints (on NT):
Thread#1 is running... This is important!
Thread#1 is running... This is important!
Thread#1 is running... This is important!
Thread#1 is running... This is important!
Thread#1 is running... This is important!
Thread#2 is running... This is important!
Thread#2 is running... This is important!
Thread#2 is running... This is NOT important!
Thread#2 is running... This is NOT important!
Thread#2 is running... This is NOT important!
Since execution preference is not guaranteed based on a Thread priority, I guess I will have to stop worrying about this behaviour stuff? Am I right?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes because thread scheduling is implementation-dependent. No need to worry about that
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic