java - The set(int index, E element) method in ArrayList does not need to be synchronized in the case of multi-threading
为情所困
为情所困 2017-05-27 17:40:40
0
2
569

I know that ArrayList is a thread-unsafe class. In multi-threaded situations, add() and remove() cannot be performed directly, but can I use the set(int index, E element) method?

      List<T> records = InstanceUtil.newArrayList();
      for (int i = 0; i < ids.getRecords().size(); i++) {
        records.add(null);
      }
      ExecutorService executorService = Executors.newFixedThreadPool(5);
      for (int i = 0; i < ids.getRecords().size(); i++) {
        final int index = i;
        executorService.execute(() -> records.set(index, queryById(ids.getRecords().get(index))));
      }
为情所困
为情所困

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!