Thursday, November 7, 2013

How does JVM handle locks



We are talking about the Hotspot Java Virtual Machine. There’re the following three types of locks performed by JVM when you try to acquire lock from java.util.concurrent.locks.Lock implementation or enter synchronized block:

Biased : sometimes even in concurrent systems there’s no contention and JVM shouldn’t borrow mutex from OS for perform locking in this case. Hotspot can operate with its own internal data structures to simulate locking in more effective way. For example, if synchronized part of code is not executed concurrently in real time, JVM assigns an owner thread id to an object used as mutex in your Java code using CAS operation and additionally stores the reentrancy count if CAS is passed. It is biased lock – the ‘lightest’ type of locks done by JVM. Reentrancy count will be updated by lock owner thread just as usual local variable without CAS. If CAS fails it means another thread already gets this lock and in this case JVM stops the mutex owner thread, flushes thread context into the main memory and checks the reentrancy count. If it’s 0 JVM escalates lock to thin type otherwise to fat (I assume the main purpose is the wait time, it should be very small if lock is thin). Note Hotspot uses the same field for storing owner thread id in mutex object as for caching identity hash code. Thus if you retrieve identity hash code on you mutex once then it will be unavailable for biased locking even it was already used as biased. More info about biased locks is described in David Dice’s blog.

Thin : it’s a simple spin lock. It helps to save time for thread context switching when time for spinning is quite small. When one thread tries to acquire an occupied mutex it spins some time until the lock will be freed. The count of spins is based on internal JVM resolution and may depend from different factors: statistics gathered by JVM about your application, count of used threads, CPU and so forth. JVM determines when thin lock becomes inefficient and escalates it to fat lock.

Fat : the ‘strongest’ type of lock when JVM requests for an OS mutex and uses OS scheduler engine for threads parkings and wake ups. It is much costly than previous types because in this case JVM should directly interact with OS every time when thread acquires and frees the lock.

Java Concurrency Tutorial – Callable, Future

One of the beautiful things about Java from its very first release was the ease with which we could write multi-threaded programs and introduce asynchronous processing into our designs. The Thread class and Runnable interface combined with Java’s memory management model meant for straightforward thread programming. But  neither the Thread class nor the Runnable interface allowed for thrown Exceptions or returned values. The lack of returned values was mildly annoying.

The lack of thrown checked exceptions was a little more serious. The contract was public void run() which meant you had to catch checked exceptions and do something with them. Even if you were careful and you stored these for later verification, you couldn’t force all uses of the class to check the exception. You could go through all your getters and throw the Exception if it existed on each one. Besides being cumbersome, even that wasn’t foolproof. You couldn’t enforce calls to any of these. Thread programmers would correctly call join() to wait for it complete and may then have gone on their merry way.

Not to worry though, after many years, this was finally addressed in the 1.5 release. With the introduction of the Callable and Future interfaces and their support in the thread pools.

Callable
The Callable interface declares public T call() throws Exception. Now we can return a result, have it strongly typed as declared in our implementation and even throw Exceptions. While there are some utility methods in the Executors class to convert your Runnable instances as discussed in Part 3, you would do well to review your current implementations of Runnable or subclasses of Thread. Why bother? Primarily to double check and remove the workaround you may have implemented to address the lack of support for thrown Exceptions. At the same time, you may wish to make use of the ability to return results right in the execution method eliminating any need to cast to retrieve values.

Future
Here’s where the combined power of the thread pools and Callable come together. Future is another new interface introduced in 1.5. When you submit a Callable to one of the thread pools, you are provided an instance of Future that is typed to the Callable you passed in. This object substitutes for an actual Thread instance that you would have used prior to 1.5. Whereas you previously had to do Thread.join() or Thread.join(long millis), now you may use them as in this example.

Example coming soon...

No comments:

Post a Comment