开发者

Do lock objects in Java need to be static?

I know in C# when you have an object you want to use as a lock for multi-threading, you should declare it as static inside of a class, which the class instance will be running in a separate thread.

Does this hold true for Java as well? Some examples online seem to declare the lock object as only final...

Edit: I have a resource that I want to limit to only one thread access at a time. A class that extends Thread will be used to create multiple instances 开发者_运维问答and started at the same time. What should I use?

Thanks.


Depends on in which context they are to be used. If you want a per-instance lock, then leave static away. If you want a per-class lock, then use static. Further indeed keep it final.


Simple answer, no. Long answer, it depends on what you want.

private static final Object STATIC_LOCK = new Object();

private final Object lock = new Object();

public void doSomething() {
    synchronized (STATIC_LOCK) {
        // At most, one thread can enter this portion
    }

    synchronized (lock) {
        // Many threads can be here at once, but only one per object of the class
    }
}

With that being said, I would recommend you look at the locks provided in java.util.concurrent.locks. Using java.util.concurrent.locks.Lock you can do the following:

Lock l = ...;
l.lock();
try {
    // access the resource protected by this lock
} finally {
    l.unlock();
}  


No

In Java it is possible to use non-static members as locks.

private Object lock = new Object();

public void test(){
    synchronized (lock) {
      // your code  
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜