简体   繁体   中英

What's the usage of synchronized in Java?

I have written several years of C++. And just begin to write in Java. There are synchronized statements in Java. It's new to me and I found the following usage:

synchronized (NetworkManager.class) {/*some code*/}

and

NetworkManager myInstance = new NetworkManager();
synchronized(myInstance){/*some code*/}

I know that synchronized statements will block other synchronized access to the synchronized sections in an object. But I'm confused by the argument of synchronized.

What I think is that if I want to block some method then I should synchronize on myInstance in C++ for second example. If I create another instance, it should have no influence. Is this correct?

If it is correct, what happens with use of NetworkManager.class ? Will the .class return a class object reference to NetworkManager . On which instances will it take effect? Or will it influence all NetworkManager instances?

synchronized (NetworkManager.class) {//some code} will acquire lock on the Class object of NetworkManager class. It will prevent conccurent access to static fields and methods of the Networkmanager class provided you have synchronized on the same Class Object (Networkmanager.class) while accessing those methods. or

NetworkManager myInstance = new NetworkManager();
synchronized(myInstance){//some code}

In the above two lines, you are locking a particular instance of NetworkManager class (not the class itself).

When using synchronized (object) the current Thread will obtain the monitor lock of the provided object (and release it when it exits the block or waits on the object). If the monitor lock is already held by a different thread, then the thread will queue for that lock and wait until it becomes available. In other words using synchronized provides exclusive access with object as mutex. Note that using synchronized also provides certain memory visibility guarantees.

When using synchronized (myInstance) you lock on the provided instance. All synchronized blocks using the same instance and the synchronized instance methods of this instance will use the same mutex.

When you use synchronized (NetworkManager.class) you lock on the Class instance of NetworkManager . All synchronized blocks using NetworkManager.class (assuming they are all from the same ClassLoader ) and static synchronized methods of NetworkManager will use the same mutex.

So using synchronized (NetworkManager.class) does not lock on all instances. The only real difference between locking on an instance or on the Class object is that the former includes locking with regard to synchronized methods of the instance, while the latter includes locking with regard to synchronized static methods of the class; in all other ways they work the same: you lock on the provided object.

You might also want to read up on the Java Concurrency tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM