用一段程序证明ArrayList是线程不安全的

众所周知arrayList是线程不安全的,如何证明之,请看以下代码,附带运行结果。

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
 * Description:验证arrayList为线程不安全的
 *
 * @author Mr Li
 * @date 2019/4/8
 **/
public class TestArrayList {
    public static void main(String[] args) {
        //新建arrayList
        List<String> list = new ArrayList<>();
        //采用多线程的方式向list中添加随机数
        for (int i = 0; i < 30; i++) {
            new Thread(()->{
                list.add(UUID.randomUUID().toString().substring(0,8));
                System.out.println(Thread.currentThread().getName() + "/t" +list);
            },String.valueOf(i)).start();
        }
    }
}

当循环次数为30次时程序运行正常,运行结果如下,之前在笔记本上运行是报错了,可能跟电脑配置有关。

1/t[69865b00]
2/t[69865b00, d4b1ec16]
0/t[69865b00, d4b1ec16, bdde35d8]
3/t[69865b00, d4b1ec16, bdde35d8, 995d421f]
4/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01]
5/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b]
7/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227]
8/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671]
9/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2]
10/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0]
15/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a]
16/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0]
20/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00]
26/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00, 4257d1a8]
27/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00, 4257d1a8, d601ed97]
28/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00, 4257d1a8, d601ed97, 41a3de9b]
11/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00, 4257d1a8, d601ed97, 41a3de9b, b2eb8328]
12/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00, 4257d1a8, d601ed97, 41a3de9b, b2eb8328, 16fc3b4a]
13/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00, 4257d1a8, d601ed97, 41a3de9b, b2eb8328, 16fc3b4a, 77fd8943]
19/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00, 4257d1a8, d601ed97, 41a3de9b, b2eb8328, 16fc3b4a, 77fd8943, fc260b81]
23/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00, 4257d1a8, d601ed97, 41a3de9b, b2eb8328, 16fc3b4a, 77fd8943, fc260b81, de04fe72]
24/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00, 4257d1a8, d601ed97, 41a3de9b, b2eb8328, 16fc3b4a, 77fd8943, fc260b81, de04fe72, 4e9376c2]
29/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00, 4257d1a8, d601ed97, 41a3de9b, b2eb8328, 16fc3b4a, 77fd8943, fc260b81, de04fe72, 4e9376c2, 362873ca]
14/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00, 4257d1a8, d601ed97, 41a3de9b, b2eb8328, 16fc3b4a, 77fd8943, fc260b81, de04fe72, 4e9376c2, 362873ca, 823eb897]
21/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00, 4257d1a8, d601ed97, 41a3de9b, b2eb8328, 16fc3b4a, 77fd8943, fc260b81, de04fe72, 4e9376c2, 362873ca, 823eb897, 099063a0]
17/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00, 4257d1a8, d601ed97, 41a3de9b, b2eb8328, 16fc3b4a, 77fd8943, fc260b81, de04fe72, 4e9376c2, 362873ca, 823eb897, 099063a0, 5b6c94cb]
25/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00, 4257d1a8, d601ed97, 41a3de9b, b2eb8328, 16fc3b4a, 77fd8943, fc260b81, de04fe72, 4e9376c2, 362873ca, 823eb897, 099063a0, 5b6c94cb, aea882e5]
6/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00, 4257d1a8, d601ed97, 41a3de9b, b2eb8328, 16fc3b4a, 77fd8943, fc260b81, de04fe72, 4e9376c2, 362873ca, 823eb897, 099063a0, 5b6c94cb, aea882e5, 48f9ad4d]
22/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00, 4257d1a8, d601ed97, 41a3de9b, b2eb8328, 16fc3b4a, 77fd8943, fc260b81, de04fe72, 4e9376c2, 362873ca, 823eb897, 099063a0, 5b6c94cb, aea882e5, 48f9ad4d, 3df34d5d]
18/t[69865b00, d4b1ec16, bdde35d8, 995d421f, ba1a6a01, e0dcda5b, 4965e227, 78f05671, bdda0ab2, fb5e82d0, 45a9c72a, b72248d0, 5a5b3f00, 4257d1a8, d601ed97, 41a3de9b, b2eb8328, 16fc3b4a, 77fd8943, fc260b81, de04fe72, 4e9376c2, 362873ca, 823eb897, 099063a0, 5b6c94cb, aea882e5, 48f9ad4d, 3df34d5d, dc664b1b]
Disconnected from the target VM, address: '127.0.0.1:61512', transport: 'socket'

当循环次数为40次时程序运行报错,运行结果如下,在执行第31个线程时报错,现在已经通过代码证明了arrayList为非线程安全的事实,如何解决这个问题后续码出。

1/t[b3c8d89e, dd1cacae]
4/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676]
2/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676]
3/t[b3c8d89e, dd1cacae, b326ae40]
0/t[b3c8d89e, dd1cacae, b326ae40, 0593771b]
5/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0]
6/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4]
7/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162]
11/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325]
12/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22]
13/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a]
18/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff]
17/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a]
19/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013]
8/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e]
9/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d]
37/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd]
16/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9]
20/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464]
22/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d]
26/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43]
27/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56]
28/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56, 2bf2389c]
24/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56, 2bf2389c, b77468cc]
30/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56, 2bf2389c, b77468cc, b4cda4d4]
29/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56, 2bf2389c, b77468cc, b4cda4d4, 9487ac79]
32/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56, 2bf2389c, b77468cc, b4cda4d4, 9487ac79, 1dda8d1c]
34/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56, 2bf2389c, b77468cc, b4cda4d4, 9487ac79, 1dda8d1c, 1abc36ad, c148d97d]
33/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56, 2bf2389c, b77468cc, b4cda4d4, 9487ac79, 1dda8d1c, 1abc36ad]
10/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56, 2bf2389c, b77468cc, b4cda4d4, 9487ac79, 1dda8d1c, 1abc36ad, c148d97d, f7e6755e]
35/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56, 2bf2389c, b77468cc, b4cda4d4, 9487ac79, 1dda8d1c, 1abc36ad, c148d97d, f7e6755e, 5ca95790]
36/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56, 2bf2389c, b77468cc, b4cda4d4, 9487ac79, 1dda8d1c, 1abc36ad, c148d97d, f7e6755e, 5ca95790, f3c9ac14]
15/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56, 2bf2389c, b77468cc, b4cda4d4, 9487ac79, 1dda8d1c, 1abc36ad, c148d97d, f7e6755e, 5ca95790, f3c9ac14, 0678f898]
38/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56, 2bf2389c, b77468cc, b4cda4d4, 9487ac79, 1dda8d1c, 1abc36ad, c148d97d, f7e6755e, 5ca95790, f3c9ac14, 0678f898, null, 4b5963b7]
39/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56, 2bf2389c, b77468cc, b4cda4d4, 9487ac79, 1dda8d1c, 1abc36ad, c148d97d, f7e6755e, 5ca95790, f3c9ac14, 0678f898, null, 4b5963b7]
21/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56, 2bf2389c, b77468cc, b4cda4d4, 9487ac79, 1dda8d1c, 1abc36ad, c148d97d, f7e6755e, 5ca95790, f3c9ac14, 0678f898, null, 4b5963b7, 4586421a]
23/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56, 2bf2389c, b77468cc, b4cda4d4, 9487ac79, 1dda8d1c, 1abc36ad, c148d97d, f7e6755e, 5ca95790, f3c9ac14, 0678f898, null, 4b5963b7, 4586421a, 727eec64]
25/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56, 2bf2389c, b77468cc, b4cda4d4, 9487ac79, 1dda8d1c, 1abc36ad, c148d97d, f7e6755e, 5ca95790, f3c9ac14, 0678f898, null, 4b5963b7, 4586421a, 727eec64, 98fb2078]
14/t[b3c8d89e, dd1cacae, b326ae40, 0593771b, da03b676, 3df367b0, 9f3072f4, 30407162, 0bb4b325, bda71d22, 12a9d44a, abc5bfff, 35ce237a, 1011b013, 50be397e, af0d4e7d, f01b5dbd, 6d82a7e9, 62dd7464, 19c2e91d, 95a8da43, 4b6e4e56, 2bf2389c, b77468cc, b4cda4d4, 9487ac79, 1dda8d1c, 1abc36ad, c148d97d, f7e6755e, 5ca95790, f3c9ac14, 0678f898, null, 4b5963b7, 4586421a, 727eec64, 98fb2078, ef2b6bb0, 2b071428]
Exception in thread "31" java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
	at java.util.ArrayList$Itr.next(ArrayList.java:859)
	at java.util.AbstractCollection.toString(AbstractCollection.java:461)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
	at com.alibaba.test.TestArrayList.lambda$main$0(TestArrayList.java:20)
	at java.lang.Thread.run(Thread.java:748)
Disconnected from the target VM, address: '127.0.0.1:61565', transport: 'socket'

解决方法请移步如何解决多线程情况下ArrayList的线程不安全

看完记得点个赞或者评论哦0.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值