*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Questions about locks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Questions about locks" Watch "Questions about locks" New topic
Author

Questions about locks

Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
I am currently reading this book by "Inside the Java Virtual Machine by Bill Venners" and on the subject of Thread, I have the following questions:
1. The book talks about class lock and object lock. Are these 2 independent of each other? If a thread obtain a class lock, it does not necessarily mean that it has an object lock as well, right?
2. The book says that "Class locks are actually implemented as object locks". So, is there another way of obtaining a class lock other than entering a Synchronized static method ?
3. Lastly, when it comes to choosing which thread to run next, the book says that:

..implementation designer can decide how to select the following:
1. A thread from the wait set given a notify command.
2. The order to resurrect threads from the wait set when given a notify command.
3. The order to allow threads from the entry set to acquire the monitor.
4. How to choose between threads suspended in the wait set after a notify command.

But I thought all along that a high priority thread is always chosen. Is the book just talking about selecting from a group of high priority threads? For example, if there are 2 high priority threads(same priority) and 1 low priority thread, the low priority thread is already out of the picture, and the JVM must only choose between the 2 high priority threads.
Thanks.
[ June 30, 2003: Message edited by: Alton Hernandez ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
1. The book talks about class lock and object lock. Are these 2 independent of each other? If a thread obtain a class lock, it does not necessarily mean that it has an object lock as well, right?
Associated with every class is a Class object. Static synchronized methods acquire the lock of the Class object for their class.

false
false
true
false
false
true
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
2. The book says that "Class locks are actually implemented as object locks". So, is there another way of obtaining a class lock other than entering a Synchronized static method ?

synchronized (ClassName.class) { /* body */ }

true
false
true
false
true
[ June 30, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
But I thought all along that a high priority thread is always chosen.

From Concurrent Programming in Java (1.1.2.3) by Doug Lea:
To make it possible to implement the Java virtual machine across diverse hardware platforms and operating systems, the Java programming language makes no promises about scheduling or fairness, and does not even strictly guarantee that threads make forward progress.
When there are more runnable threads available than available CPUs, a scheduler is generally biased to prefer running those with higher priorities. The exact policy may and does vary across platforms.
[Some JVM implementations] mix declared priorities with aging schemes or other scheduling policies to ensure that even low-priority threads eventually get a chance to run.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Hi Marlene,
Thank you for your explanations. Those are really good sample codes.
But I still have doubts on my last question. The book did say that

Threads at lower priorities are guaranteed to received CPU time only when all higher-priority threads are blocked.

In short, the highest priority thread is always the first choice. I understand that getting a CPU time is not the same as getting the lock. But given a situation where a number of threads are competing for the same lock, the JVM must choose the thread with the highest priority (assuming that all threads are equal except for its priority). It has to guarantee that it gets the CPU time.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
You might think about posting your question on the Threads forum.
The Java Programming Language (10.6) says
On a system with N available processors, you will usually see N of the highest-priority runnable threads executing. Lower-priority threads generally run only when higher-priority threads are blocked (not runnable). But lower-priority threads might, in fact, run at other times to prevent starvation�a feature generally known as priority aging�though you cannot rely on it.
In Concurrent Programming with Java (3.4.1.5), Doug Lea says
There is no way to ensure that an underlying system will ever actually execute a given runnable process or thread unless the system provides guarantees that go beyond the minimal requirements stated in the JLS. However, this is unlikely to be a significant pragmatic issue. Most if not all JVM implementations strive to provide sensible scheduling policies that extend well beyond minimal requirements. They display some sort of weak, restricted, or probabilistic fairness properties with respect to executing runnable threads. However, it is difficult for a language specification to state all the reasonable ways in which this may occur. The matter is left as a quality-of-implementation issue in the JLS.
I don't have any real-world experience with threads. I can only share what I have read. Since thread scheduling might be implemented in both JVM code and operating system kernel code, the actual scheduling policy might depend on whether and how the operating system supports multiple processors, multiple processes and/or threads.
[ June 30, 2003: Message edited by: Marlene Miller ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The book did say that
Threads at lower priorities are guaranteed to received CPU time only when all higher-priority threads are blocked.

Hurm - Bill Venners' book? I don't have a copy of that unfortunately. But the statement does not match my understanding from elsewhere. Is it possible Bill is talking about a specific JVM implementation here?


"I'm not back." - Bill Harding, Twister
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Jim Yingst:

Is it possible Bill is talking about a specific JVM implementation here?

Perhaps, so I reproduced the entire paragraph below. If you manage to get hold of a copy of that book, it's in pp. 184, Chapter 5.

The Java virtual machine specification defines the behaviour of threads at different priorities only by indicating that all threads at the highest priority will receive some CPU time. Threads at lower priorities are guaranteed to receive CPU time only when all higher-priority threads are blocked. Lower-priority threads might receive some CPU time when higher priority threads are not blocked, but there are no guarantee.

[ July 01, 2003: Message edited by: Alton Hernandez ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Alton, I have found just the right book for you on this issue of thread scheduling and priorities!
Java Threads 2nd Edition by Scott Oaks and Henry Wong, O�Reilly.
Chapter 6 Java Thread Scheduling starts out describing plain vanilla preemptive, priority-based scheduling. 12 pages later, they describe how Java thread scheduling principles have evolved from the very early days.
In the very early days of Java, priority-based thread scheduling was thought to be absolute ...
... many implementations of the Java virtual machine now allow Java threads to be scheduled directly by the operating system rather than by the virtual machine itself...
... Hence, understanding how Java threads are ultimately scheduled requires an understanding of the particular virtual machine involved. There are two basic variations here: The green-thread model ... The native-thread model...
... Later in this chapter, we�ll discuss the implementations of these thread models on several platforms and the subtle differences between these implementations with respect to thread scheduling...
... Popular Scheduling Implementations... Green Threads... Windows Native Threads... Solaris Native Threads... LWPs in the virtual machine
Pages 123 � 168.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Marlene Miller:
Alton, I have found just the right book for you on this issue of thread scheduling and priorities!
Java Threads 2nd Edition by Scott Oaks and Henry Wong, O�Reilly.

Thanks Marlene. I'll try to get a copy of that book.
Because I still have some questions about priority, I'm am going to spawn a new thread(no pun intended) from this post since the subject heading is no longer appropriate with this discussion.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Questions about locks
 
Similar Threads
Threads and Synchronization examples
Section 7 mock test questions
Sleep and Yield.. Am i correct??
What state are YOU in? (Threads)
thread problem