Java中synchronized同步锁用法及作用范围

https://blog.csdn.net/yx0628/article/details/79086511

Java 中的 synchronized 关键字可以在多线程环境下用来作为线程安全的同步锁。本文主要对 synchronized 的作用,以及其有效范围进行讨论。
Java中的对象锁和类锁:java的对象锁和类锁在锁的概念上基本上和内置锁是一致的,但是,两个锁实际是有很大的区别的,对象锁是用于对象实例方法,或者一个对象实例上的,类锁是用于类的静态方法或者一个类的class对象上的。我们知道,类的对象实例可以有很多个,但是每个类只有一个class对象,所以不同对象实例的对象锁是互不干扰的,但是每个类只有一个类锁。但是有一点必须注意的是,其实类锁只是一个概念上的东西,并不是真实存在的,它只是用来帮助我们理解锁定实例方法和静态方法的区别的。

synchronized 关键字主要有以下几种用法:
- 非静态方法的同步;
- 静态方法的同步;
- 代码块。

下面分对象锁和类锁来分别说明 synchronized 用法:

对象锁
非静态方法使用 synchronized 修饰的写法,修饰实例方法时,锁定的是当前对象:

public synchronized void test(){
// TODO
}
1
2
3
代码块使用 synchronized 修饰的写法,使用代码块,如果传入的参数是 this,那么锁定的也是当前的对象:

public void test(){
synchronized (this) {
// TODO
}
}
1
2
3
4
5
下面通过例子来说明对象锁:
定义一个类,方法如下,将 count 自减,从 5 到 0:

public class TestSynchronized {

public synchronized void minus() {
int count = 5;
for (int i = 0; i < 5; i++) {
count--;
System.out.println(Thread.currentThread().getName() + " - " + count);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
测试调用方法如下:

public class Run {

public static void main(String[] args) {

final TestSynchronized test = new TestSynchronized();

Thread thread1 = new Thread(new Runnable() {

@Override
public void run() {
test.minus();
}
});

Thread thread2 = new Thread(new Runnable() {

@Override
public void run() {
test.minus();
}
});

thread1.start();
thread2.start();

}

}
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
两个线程 thread1 和 thread2,同时访问对象的方法,由于该方法是 synchronized 关键字修饰的,那么这两个线程都需要获得该对象锁,一个获得后另一个线程必须等待。所以我们可以猜测运行结果应该是,一个线程执行完毕后,另一个线程才开始执行,运行例子,输出打印结果如下:

Thread-0 - 4
Thread-0 - 3
Thread-0 - 2
Thread-0 - 1
Thread-0 - 0
Thread-1 - 4
Thread-1 - 3
Thread-1 - 2
Thread-1 - 1
Thread-1 - 0
1
2
3
4
5
6
7
8
9
10
(另:thread1 和 thread2 谁先执行并不一定)
本例对于对象锁进行了基础的解释。但是对象锁的范围是怎样的,对象的某个同步方法被一个线程访问后,其他线程能不能访问该对象的其他同步方法,以及是否可以访问对象的其他非同步方法呢,下面对两种进行验证:

对两个同步方法两个线程的验证:
修改类如下,加入 minus2() 方法,和 minus() 方法一样:

package com.test.run;

public class TestSynchronized {

public synchronized void minus() {
int count = 5;
for (int i = 0; i < 5; i++) {
count--;
System.out.println(Thread.currentThread().getName() + " - " + count);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}

public synchronized void minus2() {
int count = 5;
for (int i = 0; i < 5; i++) {
count--;
System.out.println(Thread.currentThread().getName() + " - " + count);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}

}
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
测试调用如下,两个线程访问不同的方法:

public class Run {

public static void main(String[] args) {

final TestSynchronized test = new TestSynchronized();

Thread thread1 = new Thread(new Runnable() {

@Override
public void run() {
test.minus();
}
});

Thread thread2 = new Thread(new Runnable() {

@Override
public void run() {
test.minus2();
}
});

thread1.start();
thread2.start();
}

}
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
输出结果如下:

Thread-0 - 4
Thread-0 - 3
Thread-0 - 2
Thread-0 - 1
Thread-0 - 0
Thread-1 - 4
Thread-1 - 3
Thread-1 - 2
Thread-1 - 1
Thread-1 - 0
1
2
3
4
5
6
7
8
9
10
可以看到,某个线程得到了对象锁之后,该对象的其他同步方法是锁定的,其他线程是无法访问的。
下面看是否能访问非同步方法:
修改类代码如下,将 minus2() 的 synchronized 修饰去掉,代码如下:

public class TestSynchronized {

public synchronized void minus() {
int count = 5;
for (int i = 0; i < 5; i++) {
count--;
System.out.println(Thread.currentThread().getName() + " - " + count);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}

public void minus2() {
int count = 5;
for (int i = 0; i < 5; i++) {
count--;
System.out.println(Thread.currentThread().getName() + " - " + count);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}

}
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
测试调用的类不变,如下:

public class Run2 {

public static void main(String[] args) {

final TestSynchronized test = new TestSynchronized();

Thread thread1 = new Thread(new Runnable() {

@Override
public void run() {
test.minus();
}
});

Thread thread2 = new Thread(new Runnable() {

@Override
public void run() {
test.minus2();
}
});

thread1.start();
thread2.start();

}

}
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
执行结果如下:

Thread-1 - 4
Thread-0 - 4
Thread-1 - 3
Thread-0 - 3
Thread-1 - 2
Thread-0 - 2
Thread-1 - 1
Thread-0 - 1
Thread-1 - 0
Thread-0 - 0
1
2
3
4
5
6
7
8
9
10
可以看到,结果是交替的,说明线程是交替执行的,说明如果某个线程得到了对象锁,但是另一个线程还是可以访问没有进行同步的方法或者代码。进行了同步的方法(加锁方法)和没有进行同步的方法(普通方法)是互不影响的,一个线程进入了同步方法,得到了对象锁,其他线程还是可以访问那些没有同步的方法(普通方法)。当获取到与对象关联的内置锁时,并不能阻止其他线程访问该对象,当某个线程获得对象的锁之后,只能阻止其他线程获得同一个锁。

类锁
类锁需要 synchronized 来修饰静态 static 方法,写法如下:

public static synchronized void test(){
// TODO
}
1
2
3
或者使用代码块,需引用当前的类:

public static void test(){
synchronized (TestSynchronized.class) {
// TODO
}
}
1
2
3
4
5
举例说明类锁的作用:

public class TestSynchronized {

public static synchronized void minus() {
int count = 5;
for (int i = 0; i < 5; i++) {
count--;
System.out.println(Thread.currentThread().getName() + " - " + count);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
测试调用类如下:

public class Run {

public static void main(String[] args) {

Thread thread1 = new Thread(new Runnable() {

@Override
public void run() {
TestSynchronized.minus();
}
});

Thread thread2 = new Thread(new Runnable() {

@Override
public void run() {
TestSynchronized.minus();
}
});

thread1.start();
thread2.start();

}

}
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
输出结果如下:

Thread-0 - 4
Thread-0 - 3
Thread-0 - 2
Thread-0 - 1
Thread-0 - 0
Thread-1 - 4
Thread-1 - 3
Thread-1 - 2
Thread-1 - 1
Thread-1 - 0
1
2
3
4
5
6
7
8
9
10
可以看到,类锁和对象锁其实是一样的,由于静态方法是类所有对象共用的,所以进行同步后,该静态方法的锁也是所有对象唯一的。每次只能有一个线程来访问对象的该非静态同步方法。
类锁的作用和对象锁类似,但是作用范围是否和对象锁一致呢,下面看对象锁和类锁是否等同:
修改类,两个同步方法,其中一个是静态的:

public class TestSynchronized {

public static synchronized void minus() {
int count = 5;
for (int i = 0; i < 5; i++) {
count--;
System.out.println(Thread.currentThread().getName() + " - " + count);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}

public synchronized void minus2() {
int count = 5;
for (int i = 0; i < 5; i++) {
count--;
System.out.println(Thread.currentThread().getName() + " - " + count);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}

}
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
测试调用类如下,静态方法直接用类调用,实例方法由对象来调用:

public class Run {

public static void main(String[] args) {

final TestSynchronized test = new TestSynchronized();

Thread thread1 = new Thread(new Runnable() {

@Override
public void run() {
TestSynchronized.minus();
}
});

Thread thread2 = new Thread(new Runnable() {

@Override
public void run() {
test.minus2();
}
});

thread1.start();
thread2.start();

}

}
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
运行结果:

Thread-1 - 4
Thread-0 - 4
Thread-0 - 3
Thread-1 - 3
Thread-0 - 2
Thread-1 - 2
Thread-0 - 1
Thread-1 - 1
Thread-1 - 0
Thread-0 - 0
1
2
3
4
5
6
7
8
9
10
可以看到两个线程是交替进行的,也就是说类锁和对象锁是不一样的锁,是互相独立的。
————————————————
版权声明:本文为CSDN博主「yx0628」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yx0628/article/details/79086511

原文地址:https://www.cnblogs.com/linus-tan/p/12326710.html