views:

63

answers:

2

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.

+3  A: 

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.

cletus
+3  A: 

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?

Jon Skeet
Just some where was written that there are some levels of Synchronization,I don't know what does it mean?and also i have heard some where from my teacher and he just had a point at it and he said nothing about those levels.[:-(]
Johanna
@Johanna - where is "somewhere"? Actually, I suspect you are confused and your teacher was talking about something completely different. Anyway, the first person you should ask is your teacher ... or someone else who went to the lecture.
Stephen C