private void readAll ( ) { final List < CompletableFuture < Void > > futures = new ArrayList <> ( ) ; for ( File file : directory . listFiles ( ) ) { final Runnable loadFile = ( ) -> { if ( file . getName ( ) . endsWith ( SUFFIX ) ) { try ( FileReader reader = new FileReader ( file ) ) { Properties props = new Properties ( ) ; props . load ( reader ) ; Map . Entry < Key , HttpCookie > entry = fromProperties ( props ) ; if ( entry != null ) { add ( entry . getKey ( ) , entry . getValue ( ) ) ; } else { file . delete ( ) ; } } catch ( IOException ioe ) { throw new RuntimeException ( ioe ) ; } } } ; futures . add ( CompletableFuture . runAsync ( loadFile , executor ) ) ; } try { CompletableFuture . allOf ( futures . toArray ( new CompletableFuture [ 0 ] ) ) . get ( ) ; } catch ( InterruptedException | ExecutionException e ) { throw new RuntimeException ( e ) ; } }
each cookie store controls its own directory we do not need to synchronize