简体   繁体   English

Java中的同步锁

[英]Synchronized locks in java

I've got multi threaded application(for example, servlet). 我有多线程应用程序(例如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 ? lock应该是volatile吗?

If both threads use the same object to reference the same file, there is no need for a separate static object. 如果两个线程都使用同一对象引用同一文件,则不需要单独的static对象。 As your class is descended from Object , you can also use 由于您的课程是从Object派生的,因此您也可以使用

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. volatile包含在synchronized访问中-锁定原语意识到它们需要是线程安全的。

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. 在多线程环境中锁定资源始终是好的,但是据我所知,将资源标记为volatile并在资源上进行同步是多余的,因为两者在发布的方案中几乎完成了相同的任务。

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 . 在大多数情况下,仍然像您一样使用synchronized块也是可以的(有点像我喜欢做的一样),但是,如果遇到问题(性能,您无法完成的事情,饥饿),我建议使用类似ReentrantLock
It offers more flexibility and may offer some bonus performance. 它提供了更大的灵活性,并可能提供一些额外的性能。 Also it can accomplish things a synchronized block can not. 它还可以完成synchronized块无法完成的任务。

Also something like ReadWriteLock or ReentrantReadWriteLock could serve you well in some scenarios so they are worth reading about too. 同样,在某些情况下,诸如ReadWriteLockReentrantReadWriteLock也可以为您提供ReentrantReadWriteLock服务,因此也值得一读。

Should lock be volatile? 锁应该是易失的吗?

As far as I know there is no reason to. 据我所知,没有任何理由。 The locking objects are implied to be volatile like. 锁定对象暗示为volatile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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