@@ -1009,7 +1009,11 @@ void testAddObjectConcurrentCallsRespectsMaxIdleLimit() throws Exception {
10091009 final int numThreads = 10 ;
10101010 final CyclicBarrier barrier = new CyclicBarrier (numThreads );
10111011
1012- withConcurrentCallsRespectMaxIdle (maxIdleLimit , numThreads , pool -> getRunnables (numThreads , barrier , pool ));
1012+ withConcurrentCallsRespectMaxIdle (maxIdleLimit , numThreads , pool ->
1013+ getRunnables (numThreads , barrier , pool , (a , b ) -> {
1014+ b .await (); // Wait for all threads to be ready
1015+ a .addObject ();
1016+ }));
10131017 }
10141018
10151019
@@ -1020,7 +1024,12 @@ void testReturnObjectConcurrentCallsRespectsMaxIdleLimit() throws Exception {
10201024 final int numThreads = 200 ;
10211025 final CyclicBarrier barrier = new CyclicBarrier (numThreads );
10221026
1023- withConcurrentCallsRespectMaxIdle (maxIdleLimit , numThreads , pool -> getReturnRunnables (numThreads , barrier , pool ));
1027+ withConcurrentCallsRespectMaxIdle (maxIdleLimit , numThreads , pool ->
1028+ getRunnables (numThreads , barrier , pool , (a , b ) -> {
1029+ String pooledObject = a .borrowObject ();
1030+ b .await (); // Wait for all threads to be ready
1031+ a .returnObject (pooledObject );
1032+ }));
10241033 }
10251034
10261035 void withConcurrentCallsRespectMaxIdle (int maxIdleLimit , int numThreads , Function <GenericObjectPool <String >, List <Runnable >> operation ) throws Exception {
@@ -1049,36 +1058,21 @@ void withConcurrentCallsRespectMaxIdle(int maxIdleLimit, int numThreads, Functio
10491058 }
10501059 }
10511060
1061+ @ FunctionalInterface
1062+ public interface PoolOperation {
1063+ void execute (GenericObjectPool <String > pool , CyclicBarrier barrier ) throws Exception ;
1064+ }
10521065
10531066 private List <Runnable > getRunnables (final int numThreads ,
10541067 final CyclicBarrier barrier ,
1055- final GenericObjectPool <String > pool ) {
1056- List <Runnable > tasks = new ArrayList <>();
1057-
1058- for (int i = 0 ; i < numThreads ; i ++) {
1059- tasks .add (() -> {
1060- try {
1061- barrier .await (); // Wait for all threads to be ready
1062- pool .addObject ();
1063- } catch (Exception e ) {
1064- // do nothing
1065- }
1066- });
1067- }
1068- return tasks ;
1069- }
1070-
1071- private List <Runnable > getReturnRunnables (final int numThreads ,
1072- final CyclicBarrier barrier ,
1073- final GenericObjectPool <String > pool ) {
1068+ final GenericObjectPool <String > pool ,
1069+ final PoolOperation operation ) {
10741070 List <Runnable > tasks = new ArrayList <>();
10751071
1076- for (int i = 0 ; i < numThreads ; i ++) {
1072+ for (int i = 0 ; i < numThreads ; i ++) {
10771073 tasks .add (() -> {
10781074 try {
1079- String pooledObject = pool .borrowObject ();
1080- barrier .await (); // Wait for all threads to be ready
1081- pool .returnObject (pooledObject );
1075+ operation .execute (pool , barrier );
10821076 } catch (Exception e ) {
10831077 // do nothing
10841078 }
0 commit comments