ITeye博客

synchronized method and synchronized static method

netbean77 · 2011-09-08 10:25 · JAVA · 编程语言 · Markdown

synchronized method and synchronized static method :

 

key point1:

synchronized void foo() {
    ...
}
is same as


void foo() {
    synchronized(this) {
        ...
    }
}

 

synchronized static void foo() {
    ...
}
is same as
static void foo() {
    synchronized(SomeClass.class) {
        ...
    }
}

key point2:

 

You can use any object as the lock. If you want to lock subsets of method or static methods.

class SomeClass {
    private static final Object LOCK_1 = new Object() {};
    private static final Object LOCK_2 = new Object() {};
    void foo() {
        synchronized(LOCK_1) {...}
    }
    static void fee() {
        synchronized(LOCK_2) {...}
    }
}
refer:http://stackoverflow.com/questions/578904/how-do-synchronized-static-methods-work-in-java

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。