简体   繁体   中英

Synchronized locks in java

I've got multi threaded application(for example, servlet). Also I've got a common resource(file, ref or something). How can I synchronize its access? This is how I do:

    static Object lock = new Object();

    volatile int commonRes = 0;

    public void doSomething() {
        System.out.println("Before statement " + commonRes);

        synchronized (lock) {
            //some process
            commonRes++;
        }
        //Do something else
        System.out.println("After statement " + commonRes);
    }

I have two questions:

  1. Is it good approach?
  2. Should lock be volatile ?

If both threads use the same object to reference the same file, there is no need for a separate static object. As your class is descended from Object , you can also use

synchronized(this) { ... }

or the shorthand form covering the entire method

public synchronized void doSomething() { ... }

The volatile is implied in synchronized accesses -- the locking primitives are aware they need to be thread-safe.

Is it good approach?

Locking resources in multi-threaded environments is always good, but marking as volatile and synchronizing on the resource is redundant as far as I know since both accomplish almost the same thing in the posted scenario.

When I use synchronized blocks I prefer to lock on the class like this:

synchronized(MyClass.class) {
...
}

Why i do this:

  • both static and non-static methods will be blocked.
  • no other external object will be able to lock on your object and possibly create subtle bugs.

Still using synchronized blocks like you did is OK too in most scenarios (kinda like the same as what I prefer to do), but if you encounter problems (Performance, things you can't accomplish, starvation), I recommend using a structure like ReentrantLock .
It offers more flexibility and may offer some bonus performance. Also it can accomplish things a synchronized block can not.

Also something like ReadWriteLock or ReentrantReadWriteLock could serve you well in some scenarios so they are worth reading about too.

Should lock be volatile?

As far as I know there is no reason to. The locking objects are implied to be volatile like.

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