Hi, I have heard that there are different levels of Synchronization.are there?(If there are ,would you please explain them with a snippet code?)thanks.
Before Java 5 there was only one: the synchronized
keyword. This waited for and obtained an exclusive lock on the reference object. When applied to a function:
public synchronized void doStuff() { ... }
the object being synchronized on is this
.
Java 5 added a lot of concurrency utils, one of which was the Lock
object. There are several versions of this including a ReadWriteLock
. This is the only thing I can think of that you might be referring to.
The problem with synchronized
is that its fairly crude. Done badly it can lead to deadlocks. The Java 5 utils allow non-blocking lock acquisition, timeout on lock acquisition and read/write lock support.
You'd really need to explain what you meant by a "level of synchronization". Are you talking about the difference between:
public synchronized void foo()
{
...
}
and
public void foo()
{
synchronized(lock)
{
...
}
}
? Or perhaps between the above and using the locks from java.util.concurrent.locks?
If you could give more context to what you've heard, we may be able to help you better. More importantly, what problem are you trying to solve that you think might need this information?