浅谈多线程中的锁的几种用法总结(必看)

2025-05-29 0 68

一、ReentrantLock

?

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
package com.ietree.basicskill.mutilthread.lock;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

/**

* Created by Administrator on 2017/5/17.

*/

public class UseReentrantLock {

private Lock lock = new ReentrantLock();

public void method1(){

try {

lock.lock();

System.out.println("当前线程:" + Thread.currentThread().getName() + "进入method1..");

Thread.sleep(1000);

System.out.println("当前线程:" + Thread.currentThread().getName() + "退出method1..");

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

lock.unlock();

}

}

public void method2(){

try {

lock.lock();

System.out.println("当前线程:" + Thread.currentThread().getName() + "进入method2..");

Thread.sleep(2000);

System.out.println("当前线程:" + Thread.currentThread().getName() + "退出method2..");

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

lock.unlock();

}

}

public static void main(String[] args) {

final UseReentrantLock ur = new UseReentrantLock();

Thread t1 = new Thread(new Runnable() {

@Override

public void run() {

ur.method1();

ur.method2();

}

}, "t1");

t1.start();

try {

Thread.sleep(10);

} catch (InterruptedException e) {

e.printStackTrace();

}

//System.out.println(ur.lock.getQueueLength());

}

}

二、ReentrantReadWriteLock

?

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
package com.ietree.basicskill.mutilthread.lock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**

* Created by Administrator on 2017/5/17.

*/

public class UseReentrantReadWriteLock {

private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();

private ReentrantReadWriteLock.ReadLock readLock = rwLock.readLock();

private ReentrantReadWriteLock.WriteLock writeLock = rwLock.writeLock();

public void read(){

try {

readLock.lock();

System.out.println("当前线程:" + Thread.currentThread().getName() + "进入...");

Thread.sleep(3000);

System.out.println("当前线程:" + Thread.currentThread().getName() + "退出...");

} catch (Exception e) {

e.printStackTrace();

} finally {

readLock.unlock();

}

}

public void write(){

try {

writeLock.lock();

System.out.println("当前线程:" + Thread.currentThread().getName() + "进入...");

Thread.sleep(3000);

System.out.println("当前线程:" + Thread.currentThread().getName() + "退出...");

} catch (Exception e) {

e.printStackTrace();

} finally {

writeLock.unlock();

}

}

public static void main(String[] args) {

final UseReentrantReadWriteLock urrw = new UseReentrantReadWriteLock();

Thread t1 = new Thread(new Runnable() {

@Override

public void run() {

urrw.read();

}

}, "t1");

Thread t2 = new Thread(new Runnable() {

@Override

public void run() {

urrw.read();

}

}, "t2");

Thread t3 = new Thread(new Runnable() {

@Override

public void run() {

urrw.write();

}

}, "t3");

Thread t4 = new Thread(new Runnable() {

@Override

public void run() {

urrw.write();

}

}, "t4");

// t1.start();

// t2.start();

// t1.start(); // R

// t3.start(); // W

t3.start();

t4.start();

}

}

三、Condition

?

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
package com.ietree.basicskill.mutilthread.lock;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

/**

* Created by Administrator on 2017/5/17.

*/

public class UseCondition {

private Lock lock = new ReentrantLock();

private Condition condition = lock.newCondition();

public void method1(){

try {

lock.lock();

System.out.println("当前线程:" + Thread.currentThread().getName() + "进入等待状态..");

Thread.sleep(3000);

System.out.println("当前线程:" + Thread.currentThread().getName() + "释放..");

condition.await(); // Object wait

System.out.println("当前线程:" + Thread.currentThread().getName() +"继续执行...");

} catch (Exception e) {

e.printStackTrace();

} finally {

lock.unlock();

}

}

public void method2(){

try {

lock.lock();

System.out.println("当前线程:" + Thread.currentThread().getName() + "进入..");

Thread.sleep(3000);

System.out.println("当前线程:" + Thread.currentThread().getName() + "发出唤醒..");

condition.signal(); //Object notify

} catch (Exception e) {

e.printStackTrace();

} finally {

lock.unlock();

}

}

public static void main(String[] args) {

final UseCondition uc = new UseCondition();

Thread t1 = new Thread(new Runnable() {

@Override

public void run() {

uc.method1();

}

}, "t1");

Thread t2 = new Thread(new Runnable() {

@Override

public void run() {

uc.method2();

}

}, "t2");

t1.start();

t2.start();

}

}

四、ManyCondition

?

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

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132
package com.ietree.basicskill.mutilthread.lock;

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.ReentrantLock;

/**

* Created by Administrator on 2017/5/17.

*/

public class UseManyCondition {

private ReentrantLock lock = new ReentrantLock();

private Condition c1 = lock.newCondition();

private Condition c2 = lock.newCondition();

public void m1(){

try {

lock.lock();

System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m1等待..");

c1.await();

System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m1继续..");

} catch (Exception e) {

e.printStackTrace();

} finally {

lock.unlock();

}

}

public void m2(){

try {

lock.lock();

System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m2等待..");

c1.await();

System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m2继续..");

} catch (Exception e) {

e.printStackTrace();

} finally {

lock.unlock();

}

}

public void m3(){

try {

lock.lock();

System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m3等待..");

c2.await();

System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m3继续..");

} catch (Exception e) {

e.printStackTrace();

} finally {

lock.unlock();

}

}

public void m4(){

try {

lock.lock();

System.out.println("当前线程:" +Thread.currentThread().getName() + "唤醒..");

c1.signalAll();

} catch (Exception e) {

e.printStackTrace();

} finally {

lock.unlock();

}

}

public void m5(){

try {

lock.lock();

System.out.println("当前线程:" +Thread.currentThread().getName() + "唤醒..");

c2.signal();

} catch (Exception e) {

e.printStackTrace();

} finally {

lock.unlock();

}

}

public static void main(String[] args) {

final UseManyCondition umc = new UseManyCondition();

Thread t1 = new Thread(new Runnable() {

@Override

public void run() {

umc.m1();

}

},"t1");

Thread t2 = new Thread(new Runnable() {

@Override

public void run() {

umc.m2();

}

},"t2");

Thread t3 = new Thread(new Runnable() {

@Override

public void run() {

umc.m3();

}

},"t3");

Thread t4 = new Thread(new Runnable() {

@Override

public void run() {

umc.m4();

}

},"t4");

Thread t5 = new Thread(new Runnable() {

@Override

public void run() {

umc.m5();

}

},"t5");

t1.start(); // c1

t2.start(); // c1

t3.start(); // c2

try {

Thread.sleep(2000);

} catch (InterruptedException e) {

e.printStackTrace();

}

t4.start(); // c1

try {

Thread.sleep(2000);

} catch (InterruptedException e) {

e.printStackTrace();

}

t5.start(); // c2

}

}

以上这篇浅谈多线程中的的几种用法总结(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 浅谈多线程中的锁的几种用法总结(必看) https://www.kuaiidc.com/116577.html

相关文章

发表评论
暂无评论