1.线程安全

所谓共享的资源,是指在多个线程同时对其进行访问的情况下,各线程都会使其发生变化,而线程安全性的主要目的就在于在受控的并发访问中防止数据发生变化。除了使用synchronized关键字同步对资源的写操作之外, 还可以在线程之间不共享资源状态, 甚至将资源的状态设置为不可变。在本章中,我们将讨论如何设计不可变对象,这样就可以不用依赖于synchronized关键字的约束。

2.不可变对象的设计

Java核心类库中提供了大量的不可变对象范例, 其中java.lang.String的每一个方法都没有同步修饰, 可是其在多线程访问的情况下是安全的, Java 8中通过Stream修饰的ArrayList在函数式方法并行访问的情况下也是线程安全的, 所谓不可变对象是没有机会去修改它, 每一次的修改都会导致一个新的对象产生, 比如Strings 1=“Hello”;sl=s1+” world”两者相加会产生新的字符串。
有些非线程安全可变对象被不可变机制加以处理之后,照样也具备不可变性,比如ArrayList生成的stream在多线程的情况下也是线程安全的, 同样是因为其具备不可变性的结果,示例代码所示。

  1. import java.util.Arrays;
  2. import java.util.List;
  3. public class ArrayListStream {
  4. public static void main(String[] args) {
  5. // 定义一个list并且使用Arrays的方式进行初始化
  6. List<String> list = Arrays.asList("Java","Thread", "Concurrency","Scala","Co");
  7. // 获取并行的stream,然后通过map函数对list中的数据进行加工,最后输出
  8. list.parallelStream().map(String::toUpperCase).forEach(System.out::println);
  9. list.forEach(System.out::println);
  10. }
  11. }

2.1 非线程安全的累加器

不可变对象最核心的地方在于不给外部修改共享资源的机会,这样就会避免多线程情况下的数据冲突而导致的数据不一致的情况,又能避免因为对锁的依赖而带来的性能降低,好了, 在本节中我们将模仿java.lang.String的方式实现一个不可变的int类型累加器, 先来看看不加同步的累加器,代码如所示。

  1. import java.util.concurrent.TimeUnit;
  2. import java.util.stream.IntStream;
  3. public class IntegerAccumulator {
  4. private int init;
  5. public IntegerAccumulator(int init) {
  6. this.init = init;
  7. }
  8. public int add(int i ) {
  9. this.init += i;
  10. return this.init;
  11. }
  12. public int getValue() {
  13. return this.init;
  14. }
  15. public static void main(String[] args) {
  16. // 定义累加器,并且设计初始值为0
  17. IntegerAccumulator accumulator = new IntegerAccumulator(0);
  18. // 定义三个线程,并且分别启动
  19. IntStream.range(0,3).forEach(i->new Thread(
  20. () -> {
  21. int inc = 0;
  22. while(true) {
  23. //
  24. int oldvalue = accumulator.getValue();
  25. int result = accumulator.add(inc);
  26. System.out.println("result:" + result);
  27. if(inc + oldvalue != result ) {
  28. System.out.println("ERROR:" + oldvalue + "+" + inc + "=" + result);
  29. }
  30. inc++;
  31. slowly();
  32. }
  33. }
  34. ).start());
  35. }
  36. // 简单模拟操作的耗时
  37. private static void slowly() {
  38. try {
  39. TimeUnit.SECONDS.sleep(1);
  40. } catch (InterruptedException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. }

2.2 方法同步增加线程安全性

2.3 不可变的累加器对象设计

2.2节中通过同步的方式解决了线程安全性的问题,正确的加锁方式固然能使得一个类变成线程安全的, 比如java.utils.Vector, 但是我们需要的是设计出类似于java.lang.String的不可变类,示例代码如所示。

  1. import java.util.concurrent.TimeUnit;
  2. import java.util.stream.IntStream;
  3. public class IntegerAccumulator1 {
  4. private int init;
  5. public IntegerAccumulator1(int init) {
  6. this.init = init;
  7. }
  8. // 构造新的累加器,需要用到另一个累加
  9. public IntegerAccumulator1(IntegerAccumulator1 integerAccumulator, int init) {
  10. this.init = integerAccumulator.getValue() + init;
  11. }
  12. // public int add(int i ) {
  13. // this.init += i;
  14. // return this.init;
  15. // }
  16. public IntegerAccumulator1 add(int init) {
  17. return new IntegerAccumulator1(this, init);
  18. }
  19. public int getValue() {
  20. return this.init;
  21. }
  22. public static void main(String[] args) {
  23. // 定义累加器,并且设计初始值为0
  24. IntegerAccumulator1 accumulator = new IntegerAccumulator1(0);
  25. // 定义三个线程,并且分别启动
  26. IntStream.range(0,3).forEach(i->new Thread(
  27. () -> {
  28. int inc = 0;
  29. while(true) {
  30. //
  31. int oldvalue = accumulator.getValue();
  32. int result = accumulator.add(inc).getValue();
  33. System.out.println("result:" + result);
  34. if(inc + oldvalue != result ) {
  35. System.out.println("ERROR:" + oldvalue + "+" + inc + "=" + result);
  36. }
  37. inc++;
  38. slowly();
  39. }
  40. }
  41. ).start());
  42. }
  43. // 简单模拟操作的耗时
  44. private static void slowly() {
  45. try {
  46. TimeUnit.SECONDS.sleep(1);
  47. } catch (InterruptedException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. }

重构后的Integer Accumulator, 使用了final修饰其的目的是为了防止由于继承重写而导致失去线程安全性, 另外init属性被final修饰不允许线程对其进行改变, 在构造函数中赋值后将不会再改变。
add方法并未在原有in it的基础之上进行累加, 而是创建了一个全新的IntegerAccumulator, 并未提供任何修改原始IntegerAccumulator的机会,运行上面的程序不会出现ERROR的情况。