This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Q" Watch "Thread Q" New topic
Author

Thread Q

Eric Low
Ranch Hand

Joined: Mar 14, 2002
Posts: 41
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

Joined: Jun 27, 2001
Posts: 171
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

Joined: Mar 14, 2002
Posts: 41
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

Joined: Aug 26, 2001
Posts: 7610
Yes because thread scheduling is implementation-dependent. No need to worry about that


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Thread Q
 
Similar Threads
thread doubt
Question on threads
initialization sequence
Qn on Threads
Object Reference Casting