列表元素为AtomicLong时,会改变:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.Collections;
public class test {
public static void main(String[] args) {
List<AtomicLong> a = new ArrayList<>();
a.add(new AtomicLong(1));
System.out.println(a.get(0).get()+"\n");
AtomicLong b = a.get(0);
b.compareAndSet(1, 2);
System.out.println(a.get(0).get()+"\n");


}
}

当列表元素为Interger时,不会发生改变:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.List;

public class test {
public static void main(String[] args) {
List<Integer> a = new ArrayList<>();
a.add(1);
System.out.println(a.get(0)+"\n");
int b = a.get(0);
b = 2;
System.out.println(a.get(0)+"\n");


}
}