0

我正在尝试使用多线程从列表中删除项目,直到其为空。

代码:

    public void testUsers() {
        final List<User> users = userDao.findAll();
        final int availableProcessors = Runtime.getRuntime().availableProcessors() * multiplier;
        final List<String> loggingList = Lists.newArrayList();
        final List<Integer> sizeChecked = Lists.newArrayList();
        int totalSizeChecked = 0;
        int sizeList = users.size();
        ExecutorService executorService = Executors.newFixedThreadPool(availableProcessors);
        for (int i = 0; i < availableProcessors; i++) {
            createThread(executorService, users, loggingList, sizeChecked);
        }

        executorService.shutdown();
        try {
        // wait for all threads to die
            executorService.awaitTermination(1, TimeUnit.HOURS);
        } catch (InterruptedException ex) {
        }

        for (Integer count : sizeChecked) {
           totalSizeChecked += count;
        }
        Assert.assertTrue(totalSizeChecked==sizeList);
   }

    private void createThread(ExecutorService executorService, final List<User> users,
        final Collection<String> loggingList, final List<Integer> sizeChecked) {

        executorService.execute(new Runnable() {
            @Override
            public void run() {
                int totalChecked = 0;
                while (!users.isEmpty()) {
                    User user = null;
                    synchronized (users) {
                        if (!users.isEmpty()) {
                            user = users.remove(0);
                        }
                    }
                    totalChecked++;
                    if (user != null) {
                        String reason = checkUser(user);
                        if (reason != null) {
                            loggingList.add(reason);
                        }
                    } else {
                        LOGGER.info("user is null");
                    }

                }
                sizeChecked.add(totalChecked);
            }
        });
    }

现在我认为这不可能是错误的,因为我使列表同步以删除第一项。我正在使用 6 的乘数进行测试。(在 prod 上它将降低到 1-2)我在电子邮件中收到:批次未正确执行。必须检查的帐户大小:28499。已检查的帐户大小:25869

让它线程安全有什么错?

4

1 回答 1

1

List<Integer> sizeChecked不是线程安全的。因此,您不能在其中并行添加元素。

同步您的add操作或使用线程安全结构。如果sizeChecked只是一个计数器,请使用 anAtomicLong并让每个线程递增它。

于 2013-10-29T09:46:31.027 回答