Java线程之锁对象Lock-同步问题更完美的处理方式代码实例

2025-05-27 0 38

Lock是java.util.concurrent.locks包下的接口,Lock 实现提供了比使用synchronized 方法和语句可获得的更广泛的锁定操作,它能以更优雅的方式处理线程同步问题,我们拿Java线程之线程同步synchronized和volatile详解中的一个例子简单的实现一下和sychronized一样的效果,代码如下:

?

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 LockTest {

public static void main(String[] args) {

final Outputter1 output = new Outputter1();

new Thread() {

public void run() {

output.output("zhangsan");

};

}.start();

new Thread() {

public void run() {

output.output("lisi");

};

}.start();

}

}

class Outputter1 {

private Lock lock = new ReentrantLock();// 锁对象

public void output(String name) {

// TODO 线程输出方法

lock.lock();// 得到锁

try {

for(int i = 0; i < name.length(); i++) {

System.out.print(name.charAt(i));

}

} finally {

lock.unlock();// 释放锁

}

}

}

这样就实现了和sychronized一样的同步效果,需要注意的是,用sychronized修饰的方法或者语句块在代码执行完之后锁自动释放,而用Lock需要我们手动释放锁,所以为了保证锁最终被释放(发生异常情况),要把互斥区放在try内,释放锁放在finally内。

如果说这就是Lock,那么它不能成为同步问题更完美的处理方式,下面要介绍的是读写锁(ReadWriteLock),我们会有一种需求,在对数据进行读写的时候,为了保证数据的一致性和完整性,需要读和写是互斥的,写和写是互斥的,但是读和读是不需要互斥的,这样读和读不互斥性能更高些,来看一下不考虑互斥情况的代码原型:

?

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
public class ReadWriteLockTest {

public static void main(String[] args) {

final Data data = new Data();

for (int i = 0; i < 3; i++) {

new Thread(new Runnable() {

public void run() {

for (int j = 0; j < 5; j++) {

data.set(new Random().nextInt(30));

}

}

}).start();

}

for (int i = 0; i < 3; i++) {

new Thread(new Runnable() {

public void run() {

for (int j = 0; j < 5; j++) {

data.get();

}

}

}).start();

}

}

}

class Data {

private int data;// 共享数据

public void set(int data) {

System.out.println(Thread.currentThread().getName() + "准备写入数据");

try {

Thread.sleep(20);

} catch (InterruptedException e) {

e.printStackTrace();

}

this.data = data;

System.out.println(Thread.currentThread().getName() + "写入" + this.data);

}

public void get() {

System.out.println(Thread.currentThread().getName() + "准备读取数据");

try {

Thread.sleep(20);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName() + "读取" + this.data);

}

}

部分输出结果:

?

1

2

3

4

5

6

7

8

9

10
Thread-1准备写入数据

Thread-3准备读取数据

Thread-2准备写入数据

Thread-0准备写入数据

Thread-4准备读取数据

Thread-5准备读取数据

Thread-2写入12

Thread-4读取12

Thread-5读取5

Thread-1写入12

我们要实现写入和写入互斥,读取和写入互斥,读取和读取互斥,在set和get方法加入sychronized修饰符:

?

1

2
public synchronized void set(int data) {...}

public synchronized void get() {...}

部分输出结果:

?

1

2

3

4

5

6

7

8

9

10
Thread-0准备写入数据

Thread-0写入9

Thread-5准备读取数据

Thread-5读取9

Thread-5准备读取数据

Thread-5读取9

Thread-5准备读取数据

Thread-5读取9

Thread-5准备读取数据

Thread-5读取9

我们发现,虽然写入和写入互斥了,读取和写入也互斥了,但是读取和读取之间也互斥了,不能并发执行,效率较低,用读写锁实现代码如下:

?

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
class Data {

private int data;// 共享数据

private ReadWriteLock rwl = new ReentrantReadWriteLock();

public void set(int data) {

rwl.writeLock().lock();// 取到写锁

try {

System.out.println(Thread.currentThread().getName() + "准备写入数据");

try {

Thread.sleep(20);

} catch (InterruptedException e) {

e.printStackTrace();

}

this.data = data;

System.out.println(Thread.currentThread().getName() + "写入" + this.data);

} finally {

rwl.writeLock().unlock();// 释放写锁

}

}

public void get() {

rwl.readLock().lock();// 取到读锁

try {

System.out.println(Thread.currentThread().getName() + "准备读取数据");

try {

Thread.sleep(20);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName() + "读取" + this.data);

} finally {

rwl.readLock().unlock();// 释放读锁

}

}

}

部分输出结果:

?

1

2

3

4

5

6

7

8

9

10

11

12

13
Thread-4准备读取数据

Thread-3准备读取数据

Thread-5准备读取数据

Thread-5读取18

Thread-4读取18

Thread-3读取18

Thread-2准备写入数据

Thread-2写入6

Thread-2准备写入数据

Thread-2写入10

Thread-1准备写入数据

Thread-1写入22

Thread-5准备读取数据

从结果可以看出实现了我们的需求,这只是锁的基本用法,锁的机制还需要继续深入学习。

总结

以上就是本文关于Java线程之锁对象Lock-同步问题更完美的处理方式代码实例的全部内容,希望对大家有所帮助,有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/ghsau/article/details/7461369

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java线程之锁对象Lock-同步问题更完美的处理方式代码实例 https://www.kuaiidc.com/77490.html

相关文章

发表评论
暂无评论