Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between synchronized on lockObject and using this as the lock?

I know the difference between synchronized method and synchronized block but I am not sure about the synchronized block part.

Assuming I have this code

class Test {   private int x=0;   private Object lockObject = new Object();    public void incBlock() {     synchronized(lockObject) {       x++;     }     System.out.println("x="+x);   }    public void incThis() {  // same as synchronized method     synchronized(this) {       x++;     }     System.out.println("x="+x);   } } 

In this case what is the difference between using lockObject and using this as the lock? It seems to be the same to me..

When you decide to use synchronized block, how do you decide which object to be the lock?

like image 402
GantengX Avatar asked Jul 30 '10 07:07

GantengX


People also ask

What is the difference between synchronized and lock?

Major difference between lock and synchronized: with locks, you can release and acquire the locks in any order. with synchronized, you can release the locks only in the order it was acquired.

What are the advantages of using a lock instead of synchronization?

The major advantage of lock interfaces on multi-threaded and concurrent programming is they provide two separate lock for reading and writing which enables you to write high-performance data structure like ConcurrentHashMap and conditional blocking. A thread can take a lock only once.

What does synchronized lock do?

A synchronized block in Java is synchronized on some object. All synchronized blocks synchronized on the same object can only have one thread executing inside them at the same time. All other threads attempting to enter the synchronized block are blocked until the thread inside the synchronized block exits the block.

What is the difference between synchronized and non synchronized?

Non synchronized -It is not-thread safe and can't be shared between many threads without proper synchronization code. While, Synchronized- It is thread-safe and can be shared with many threads.


1 Answers

Personally I almost never lock on "this". I usually lock on a privately held reference which I know that no other code is going to lock on. If you lock on "this" then any other code which knows about your object might choose to lock on it. While it's unlikely to happen, it certainly could do - and could cause deadlocks, or just excessive locking.

There's nothing particularly magical about what you lock on - you can think of it as a token, effectively. Anyone locking with the same token will be trying to acquire the same lock. Unless you want other code to be able to acquire the same lock, use a private variable. I'd also encourage you to make the variable final - I can't remember a situation where I've ever wanted to change a lock variable over the lifetime of an object.

like image 58
Jon Skeet Avatar answered Oct 14 '22 02:10

Jon Skeet