You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2022/10/19 13:07:59 UTC

[GitHub] [commons-pool] aooohan commented on pull request #182: Avoid potential ConcurrentModificationException by using Iterator.

aooohan commented on PR #182:
URL: https://github.com/apache/commons-pool/pull/182#issuecomment-1283987610

   Hi @garydgregory 
   
   Sorry for the late reply.
   
   The following is unit test.
   ```java
           try (final GenericObjectPool<String, RuntimeException> pool = new GenericObjectPool<>(new BasePooledObjectFactory<String, RuntimeException>() {
               @Override
               public String create() {
                   return null;
               }
               @Override
               public PooledObject<String> wrap(final String obj) {
                   return new DefaultPooledObject<>(obj);
               }
           })) {
               for (int i = 0; i < 1000; i++) {
                   final BaseGenericObjectPool<String, RuntimeException>.Evictor evictor2 = pool.new Evictor();
                   EvictionTimer.schedule(evictor2, Duration.ofSeconds(10), Duration.ofSeconds(30));
               }
               System.gc();
               System.out.println("gc结束");
               TimeUnit.SECONDS.sleep(100);
           }
   ```
   There are two steps:
   
   1. If you run it straight away, nothing appears to be happening.
   2.  if you modify `Reaper` a bit, you'll see that it actually throws a `ConcurrentModificationException`, but I haven't figured out why the exception isn't thrown in the first step.
   ```java
   private static class Reaper implements Runnable {
           @Override
           public void run() {
               synchronized (EvictionTimer.class) {
                   try {
                       for (final Entry<WeakReference<BaseGenericObjectPool<?, ?>.Evictor>, WeakRunner<BaseGenericObjectPool<?, ?>.Evictor>> entry : TASK_MAP
                               .entrySet()) {
                           if (entry.getKey().get() == null) {
                               executor.remove(entry.getValue());
                               TASK_MAP.remove(entry.getKey());
                           }
                       }
                   } catch (Exception e) {
                       e.printStackTrace();
                       System.out.println(e);
                   }
                   if (TASK_MAP.isEmpty() && executor != null) {
                       executor.shutdown();
                       executor.setCorePoolSize(0);
                       executor = null;
                   }
               }
           }
       }
   ```
   
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org