Java语言中synchronized修饰在static方法和非static方法的区别

大家都知道,在Java中,synchronized 是用来表示同步的,我们可以synchronized 来修饰一个方法(实例方法和类方法—注:不知道这样叫准确不准确,大家理解我的意识就行了)。也可以synchronized 来修饰方法里面的一个语句块。

修饰实例方法:

1
2
3
4
5
6
7
8
public synchronized void x() throws InterruptedException
{
for (int i = 0; i < 10; i++)
{
Thread.sleep(1000);
System.out.println("x.......................");
}
}

修饰类方法(static 方法):

1
2
3
4
5
6
7
8
public static synchronized void staticX() throws InterruptedException
{
for (int i = 0; i < 10; i++)
{
Thread.sleep(1000);
System.out.println("staticX.......................");
}
}

修饰方法里面语句块:

1
2
3
4
5
6
7
8
9
10
11
public static void staticX() throws InterruptedException
{
synchronized (locks)
{
for (int i = 0; i < 10; i++)
{
Thread.sleep(1000);
System.out.println("staticX.......................");
}
}
}

注意:这里不能用synchronized修饰方法外面的语句块(我把他叫做类语句块),虽然我们可以在方法外面定义语句块,这样做会遇到编译错误,这里涉及到了Java里面的对象初始化的部分知识。大概的原因就是synchronized锁住的是对象,当初始化对象的时候,JVM在对象初始化完成之前会调用方法外面的语句块,这个时候对象还不存在,所以就不存在锁了。

那么,在static方法和非static方法前面加synchronized到底有什么不同呢?

大家都知道,static的方法属于类方法,它属于这个Class(注意:这里的Class不是指Class的某个具体对象),那么static获取到的锁,就是当前调用这个方法的对象所属的类(Class,而不再是由这个Class产生的某个具体对象了)。而非static方法获取到的锁,就是当前调用这个方法的对象的锁了。所以,他们之间不会产生互斥。

看代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.jack.zhang.chapter9.classlock;

/**
* @author Jack Zhang
* @version vb1.0
* @Email virgoboy2004@163.com
* @Date 2012-5-20
*/
public class Test
{
public static synchronized void staticX() throws InterruptedException
{
for (int i = 0; i < 10; i++)
{
Thread.sleep(1000);
System.out.println("staticX.......................");
}
}

public synchronized void x() throws InterruptedException
{
for (int i = 0; i < 10; i++)
{
Thread.sleep(1000);
System.out.println("x.......................");
}
}

public static void main(String[] args)
{
final Test test1 = new Test();
Thread thread = new Thread(new Runnable()
{
public void run()
{
try
{
test1.x();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, "a");

Thread thread1 = new Thread(new Runnable()
{
public void run()
{
try
{
Test.staticX();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, "b");

thread1.start();
thread.start();
}
}

运行结果是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
staticX.......................
x.......................
x.......................
staticX.......................
staticX.......................
x.......................
x.......................
staticX.......................
x.......................
staticX.......................
staticX.......................
x.......................
x.......................
staticX.......................
x.......................
staticX.......................
x.......................
staticX.......................
x.......................
staticX.......................

那当我们想让所有这个类下面的对象都同步的时候,也就是让所有这个类下面的对象共用同一把锁的时候,我们如何办呢?

看代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.jack.zhang.chapter9.classlock;


/**
* @author Jack Zhang
* @version vb1.0
* @Email virgoboy2004@163.com
* @Date 2012-5-20
*/
public class Test
{
public final static Byte[] locks = new Byte[0];

public static void staticX() throws InterruptedException
{
synchronized (locks)
{
for (int i = 0; i < 10; i++)
{
Thread.sleep(1000);
System.out.println("staticX.......................");
}
}
}

public void x() throws InterruptedException
{
synchronized (locks)
{
for (int i = 0; i < 10; i++)
{
Thread.sleep(1000);
System.out.println("x.......................");
}
}
}

public static void main(String[] args)
{
final Test test1 = new Test();
final Test test2 = new Test();
Thread thread = new Thread(new Runnable()
{
public void run()
{
try
{
test1.x();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, "a");

Thread thread1 = new Thread(new Runnable()
{
public void run()
{
try
{
Test.staticX();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, "b");

thread1.start();
thread.start();
}
}

运行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
staticX.......................
staticX.......................
staticX.......................
staticX.......................
staticX.......................
staticX.......................
staticX.......................
staticX.......................
staticX.......................
staticX.......................
x.......................
x.......................
x.......................
x.......................
x.......................
x.......................
x.......................
x.......................
x.......................
x.......................