---
title: "synchronized method and synchronized static method"
author: "netbean77"
date: "2011-09-08T10:25:46Z"
tags: []
categories: ["编程语言"]
slug: "synchronized-method-and-synchronized-static-method"
draft: false
lastmod: "2011-09-08T10:26:39Z"
id: 1168238
url: "https://netbean77.iteye.com/blog/1168238"
---

synchronized method and synchronized static method :

 

key point1:

```java
synchronized void foo() {
    ...
}
```

```java
is same as
```

```java


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

 

```java
synchronized static void foo() {
    ...
}
```

```java
is same as
```

```java
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.

```java
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) {...}
    }
}
```

```java
refer:http://stackoverflow.com/questions/578904/how-do-synchronized-static-methods-work-in-java
```