Java 多线程与高并发

网友投稿 482 2022-05-30

保证线程可见性

当多个线程访问同一个共享资源时,线程会拷贝资源的副本到自己的工作内存。这样如果某个线程对这个资源进行写操作,其他线程不会马上知道。当对这个资源加volatile关键字,其他线程就会随时监听,更新新的值。

如下例子,不加volatile关键字,线程不会停止,加volatile关键字后会及时重新更新副本stop的值,线程停止。

package com.nobody.thread; /** * 不加volatile,输出: * main start... * thread start... * change stop=true * * 加volatile,输出: * main start... * thread start... * thread stop... * change stop=true * @author Μr.ηobοdy * * @date 2020-04-19 * */ public class VolatileDemo { private /* volatile */ static boolean stop = false; public static void main(String[] args) { Thread t = new Thread(() -> { System.out.println("thread start..."); while (!stop) { } System.out.println("thread stop..."); }); System.out.println("main start..."); t.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } stop = true; System.out.println("change stop=" + stop); } }

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

禁止指令重排序

JIT(即时编译器just-in-time compiler) 是一种提高程序运行效率的方法,会将指令重排序。例如实例化一个对象,一般可分为3步骤,第一分配内存空间,第二初始化变量等,第三将引用地址赋值给引用对象。指令重排序可将顺序改为132。这样引用对象可能就拿到一个未初始化的对象,导致出错。

package com.nobody.thread; /** * 单例模式(懒汉式) * 懒汉式必须加volatile * * @author Μr.ηobοdy * * @date 2020-04-19 * */ public class Singleton { private /* vovalite */ static Singleton INSTANCE; private String name; private Singleton(String name) { this.name = name; } public static Singleton getInstance() { if (null == INSTANCE) { synchronized (Singleton.class) { if (null == INSTANCE) { // 可能会出现指令重排序,即未进行成员变量name的初始化就退出了, // 这样别人就会拿到未初始化(name=null)的Singleton对象 INSTANCE = new Singleton("hh"); } } } return INSTANCE; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

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

不保证原子性

package com.nobody.thread; import java.util.ArrayList; import java.util.List; /** * volatile不保证原子性,最终结果一般小于10000 * * 若要保证原子性,直接将doCount方法加synchronized关键字即可,而volatile可有可无 * * @author Μr.ηobοdy * * @date 2020-04-19 * */ public class VolatileDemo1 { private volatile static int count = 0; private /*synchronized*/ void doCount() { for (int i = 0; i < 1000; i++) { count++; } } public static void main(String[] args) { VolatileDemo1 v = new VolatileDemo1(); // 启动10个线程 List threads = new ArrayList<>(); for (int i = 1; i <= 10; i++) { threads.add(new Thread(v::doCount, "thread-" + i)); } threads.forEach(t -> t.start()); // 等待10个线程执行完 threads.forEach(t -> { try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } }); System.out.println("count=" + count); } }

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

设置新值之前会先将旧的值与期望值比较,如果相等才set,不然就重试或者失败。这是有CPU原语支持的。

package com.nobody.thread; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; /** * CAS AtomicInteger保证原子性,最终结果一定等于10000 * * * @author Μr.ηobοdy * * @date 2020-04-19 * */ public class AtomicIntegerDemo { private static AtomicInteger count = new AtomicInteger(0); private void doCount() { for (int i = 0; i < 1000; i++) { count.incrementAndGet(); } } public static void main(String[] args) { AtomicIntegerDemo v = new AtomicIntegerDemo(); // 启动10个线程 List threads = new ArrayList<>(); for (int i = 1; i <= 10; i++) { threads.add(new Thread(v::doCount, "thread-" + i)); } threads.forEach(t -> t.start()); // 等待10个线程执行完 threads.forEach(t -> { try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } }); System.out.println("count=" + count); } }

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

不过这种会出现ABA问题,即由值A先变成值B,然后又变回A值,最后旧值与期望值比较还是相等。可用版本号解决这个问题。

采用分段锁思想,假如有1000个线程对同一个共享变量进行操作(例如自增),此处假设分为4小组,250个线程为1组,组内进行自增操作,这样分组能减少锁的概率,最后将每个小组进行求总和处理。其实分段锁组内还是CAS原理。一般在线程数高时,效率比synchronized和AtomicLong高。

package com.nobody.thread; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.LongAdder; /** * LongAdder,AtomicLong,synchronized多线程时效率比较 * 模拟1000个线程对一个等于0的值进行自增操作,每个线程自增10000 * * 输出结果: * longAdderCount:10000000, time:227 * atomicLongCount:10000000, time:395 * synchronizedCount:10000000, time:909 * * @author Μr.ηobοdy * * @date 2020-04-20 * */ public class LongAdderDemo { private static LongAdder longAdderCount = new LongAdder(); private static AtomicLong atomicLongCount = new AtomicLong(0L); private static long synchronizedCount = 0L; public static void main(String[] args) { // LongAdder测试 List longAdderThreads = new ArrayList<>(1000); for (int i = 1; i <= 1000; i++) { longAdderThreads.add(new Thread(() -> { for (int j = 0; j < 10000; j++) { longAdderCount.increment(); } })); } long start = System.currentTimeMillis(); longAdderThreads.forEach(t -> t.start()); // 等待1000个线程执行完 longAdderThreads.forEach(t -> { try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } }); long end = System.currentTimeMillis(); // AtomicLong测试 List atomicLongThreads = new ArrayList<>(1000); for (int i = 1; i <= 1000; i++) { atomicLongThreads.add(new Thread(() -> { for (int j = 0; j < 10000; j++) { atomicLongCount.incrementAndGet(); } })); } long start1 = System.currentTimeMillis(); atomicLongThreads.forEach(t -> t.start()); // 等待1000个线程执行完 atomicLongThreads.forEach(t -> { try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } }); long end1 = System.currentTimeMillis(); // AtomicLong测试 List synchronizedThreads = new ArrayList<>(1000); Object o = new Object(); for (int i = 1; i <= 1000; i++) { synchronizedThreads.add(new Thread(() -> { for (int j = 0; j < 10000; j++) { synchronized (o) { synchronizedCount++; } } })); } long start2 = System.currentTimeMillis(); synchronizedThreads.forEach(t -> t.start()); // 等待1000个线程执行完 synchronizedThreads.forEach(t -> { try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } }); long end2 = System.currentTimeMillis(); System.out.println("longAdderCount:" + longAdderCount + ", time:" + (end - start)); System.out.println("atomicLongCount:" + atomicLongCount + ", time:" + (end1 - start1)); System.out.println("synchronizedCount:" + synchronizedCount + ", time:" + (end2 - start2)); } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Java 多线程与高并发

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

Java 任务调度 多线程

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:信号量--System V信号量 与 Posix信号量
下一篇:华为云,一直有“上天”的梦想
相关文章