4343import java .util .Timer ;
4444import java .util .TimerTask ;
4545import java .util .concurrent .CountDownLatch ;
46+ import java .util .concurrent .CyclicBarrier ;
4647import java .util .concurrent .ExecutorService ;
4748import java .util .concurrent .Executors ;
4849import java .util .concurrent .FutureTask ;
@@ -1012,18 +1013,21 @@ void testAddObjectConcurrentCallsRespectsMaxIdle() throws Exception {
10121013 pool .setMaxIdle (maxIdleLimit );
10131014 pool .setMaxTotal (-1 );
10141015
1015- final CountDownLatch startLatch = new CountDownLatch ( 1 );
1016- List <Runnable > tasks = getRunnables (numThreads , startLatch , pool );
1016+ final CyclicBarrier barrier = new CyclicBarrier ( numThreads );
1017+ List <Runnable > tasks = getRunnables (numThreads , barrier , pool );
10171018
10181019 ExecutorService executorService = Executors .newFixedThreadPool (numThreads );
1019- tasks .forEach (executorService ::submit );
10201020 try {
1021- startLatch .countDown (); // Start all threads simultaneously
1022- } finally {
1021+ tasks .forEach (executorService ::submit );
10231022 executorService .shutdown ();
1024- assertTrue (executorService .awaitTermination (Long .MAX_VALUE , TimeUnit .NANOSECONDS ));
1023+ assertTrue (executorService .awaitTermination (60 , TimeUnit .SECONDS ),
1024+ "Executor did not terminate in time" );
1025+ } finally {
1026+ executorService .shutdownNow (); // Ensure cleanup
10251027 }
10261028
1029+ assertTrue (executorService .awaitTermination (Long .MAX_VALUE , TimeUnit .NANOSECONDS ));
1030+
10271031 assertTrue (pool .getNumIdle () <= maxIdleLimit ,
10281032 "Concurrent addObject() calls should not exceed maxIdle limit of " + maxIdleLimit +
10291033 ", but found " + pool .getNumIdle () + " idle objects" );
@@ -1037,22 +1041,23 @@ void testReturnObjectRespectsMaxIdleLimit() throws Exception {
10371041 config .setJmxEnabled (false );
10381042 try (final GenericObjectPool <String > pool = new GenericObjectPool <>(new SimpleFactory (), config )) {
10391043 assertEquals (0 , pool .getNumIdle (), "should be zero idle" );
1040- final int maxIdleLimit = 5 ;
1041- final int numThreads = 100 ;
1044+ final int maxIdleLimit = 10 ;
1045+ final int numThreads = 200 ;
10421046
10431047 pool .setMaxTotal (-1 );
10441048 pool .setMaxIdle (maxIdleLimit );
10451049
1046- final CountDownLatch startLatch = new CountDownLatch ( 1 );
1047- List <Runnable > tasks = getReturnRunnables (numThreads , startLatch , pool );
1050+ final CyclicBarrier barrier = new CyclicBarrier ( numThreads );
1051+ List <Runnable > tasks = getReturnRunnables (numThreads , barrier , pool );
10481052
10491053 ExecutorService executorService = Executors .newFixedThreadPool (numThreads );
1050- tasks .forEach (executorService ::submit );
10511054 try {
1052- startLatch .countDown (); // Start all threads simultaneously
1053- } finally {
1055+ tasks .forEach (executorService ::submit );
10541056 executorService .shutdown ();
1055- assertTrue (executorService .awaitTermination (Long .MAX_VALUE , TimeUnit .NANOSECONDS ));
1057+ assertTrue (executorService .awaitTermination (60 , TimeUnit .SECONDS ),
1058+ "Executor did not terminate in time" );
1059+ } finally {
1060+ executorService .shutdownNow (); // Ensure cleanup
10561061 }
10571062
10581063 assertEquals (maxIdleLimit , pool .getNumIdle (),
@@ -1061,14 +1066,14 @@ void testReturnObjectRespectsMaxIdleLimit() throws Exception {
10611066 }
10621067
10631068 private List <Runnable > getRunnables (final int numThreads ,
1064- final CountDownLatch startLatch ,
1069+ final CyclicBarrier barrier ,
10651070 final GenericObjectPool <String > pool ) {
10661071 List <Runnable > tasks = new ArrayList <>();
10671072
10681073 for (int i = 0 ; i < numThreads ; i ++) {
10691074 tasks .add (() -> {
10701075 try {
1071- startLatch .await (); // Wait for all threads to be ready
1076+ barrier .await (); // Wait for all threads to be ready
10721077 pool .addObject ();
10731078 } catch (Exception e ) {
10741079 // do nothing
@@ -1079,15 +1084,15 @@ private List<Runnable> getRunnables(final int numThreads,
10791084 }
10801085
10811086 private List <Runnable > getReturnRunnables (final int numThreads ,
1082- final CountDownLatch startLatch ,
1087+ final CyclicBarrier barrier ,
10831088 final GenericObjectPool <String > pool ) {
10841089 List <Runnable > tasks = new ArrayList <>();
10851090
10861091 for (int i = 0 ; i < numThreads ; i ++) {
10871092 tasks .add (() -> {
10881093 try {
10891094 String pooledObject = pool .borrowObject ();
1090- startLatch .await (); // Wait for all threads to be ready
1095+ barrier .await (); // Wait for all threads to be ready
10911096 pool .returnObject (pooledObject );
10921097 } catch (Exception e ) {
10931098 // do nothing
0 commit comments