synchronized锁

修饰实例方法:

1
2
3
public synchronized void method(){
...
}

被锁的对象是类的实例对象,例如

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
SynContainer container = new SynContainer();

new Productor(container).start();
new Consumer(container).start();


//生产者
class Productor extends Thread(){
SynContainer container;

public Productor(SynContainer container){
this.SynContainer = container;
}
}

//消费者
class Consumer extends Thread(){
SynContainer container;

public Productor(SynContainer container){
this.SynContainer = container;
}
}

class SynContainer(){
public synchronized void method(){
...
}
}

//当运行这两个生产者和消费者时,锁住的是SynContainer的实例container,同一时刻只能有一个线程访问这个实例

修饰静态方法:

1
2
3
public static synchronized void method(){
...
}

被锁的对象是类对象

修饰实例对象:

1
2
3
synchronized (this){
...
}

同步代码块,锁住的是该类的实例对象

修饰class对象:

1
2
3
synchronized (SynContainer.class){
...
}

同步代码块,锁住的是该类的类对象

修饰任意实例对象Object:

1
2
3
4
int num = 10;
synchronize (num){
...
}

同步代码块,锁住的是配置的实例对象

int对象作为锁


Lock锁

ReentrantLock锁

相比于synchronized锁,Lock锁是显示的,能够显示的定义同步锁来实现同步

ReentrantLock(可重入锁)类实现了Lock,它拥有与synchronized相同的并发性和内存语义,在实现线程安全的控制中,比较常用的是ReentrantLock,可以显示加锁、释放锁

1
2
3
4
5
reentrantLock.lock();

...

reentrantLock.unlock();

例如:

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
public class Test {
public static void main(String[] args) {
TestLock1 lock1 = new TestLock1();

new Thread(lock1).start();
new Thread(lock1).start();
new Thread(lock1).start();
}
}

class TestLock1 implements Runnable{

int num = 10;

//定义可重入锁
private ReentrantLock reentrantLock = new ReentrantLock();

@Override
public void run() {
while (true){
try {
reentrantLock.lock(); //加锁
if (num>0){
System.out.println(num--);
}else {
break;
}
}finally {
reentrantLock.unlock(); //解锁
}

}
}
}

生产者消费者问题(管程法)

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
public class Test {
public static void main(String[] args) {
SynContainer container = new SynContainer();

new Productor(container).start();
new Consumer(container).start();
}
}

//生产者
class Productor extends Thread{
SynContainer container;

public Productor(SynContainer container){
this.container = container;
}

@Override
public void run() {
for (int i = 0; i<100 ; i++)
container.push(new Chicken(i));
}
}

//消费者
class Consumer extends Thread{
SynContainer container;

public Consumer(SynContainer container){
this.container = container;
}

@Override
public void run() {
for (int i = 0 ; i<100 ; i++){
container.pop();
}
}
}

//产品
class Chicken{
int id ; //产品编号

public Chicken(int id){
this.id = id;
}
}

//缓冲区
class SynContainer{
//容器大小
Chicken[] chickens = new Chicken[10];
//容器计数器
int count = 0;

//生产者放入产品
public synchronized void push(Chicken chicken){
//如果容器满了,就需要等待消费者消费
//最好不要用if,应该用while,否则当有多个消费者的时候,会出现脏判断的
while (count == chickens.length){
try {
this.wait();//阻塞此线程,并释放锁
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果容器没满,生产者放入产品
chickens[count] = chicken;
count++;

//通知消费者消费产品
this.notifyAll(); //唤醒其他等待线程
}

//消费者消费产品
public synchronized Chicken pop(){
//判断能否消费
if (count == 0){
//等待生产者生产,消费者等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

//如果可以消费
count--;
Chicken chicken = chickens[count];

//通知生产者生产
this.notifyAll();

return chicken;
}
}