publicclassMain{ publicstaticvoidmain(String[] args){ //这里使用Copy-on-write final List<Integer> list = new CopyOnWriteArrayList<Integer>(); new WriterThread(list).start(); new ReaderThread(list).start(); } }
final字段赋值有两种方式: 1.字段声明的时候赋值 class Something{ final int value = 123; } 2.字段在构造方法中赋值 class Something{ final int value; Something{ this.value = 123; } } final静态字段赋值两种方法: 1.字段声明时赋值 class Something{ static final int value = 123; } 2.静态代码块中赋值 class Something{ static final int value; static { value = 123; }