4444import java .util .Collections ;
4545import java .util .List ;
4646import java .util .Locale ;
47- import java .util .concurrent .*;
47+ import java .util .concurrent .Callable ;
48+ import java .util .concurrent .ExecutorService ;
49+ import java .util .concurrent .Executors ;
50+ import java .util .concurrent .TimeUnit ;
4851import java .util .stream .Collectors ;
4952import java .util .stream .Stream ;
5053
@@ -197,12 +200,14 @@ private static void doConversion(
197200 LOGGER .info ("Downgrading classes with {} threads" , threadCount );
198201 final ExecutorService threadPool = Executors .newFixedThreadPool (threadCount );
199202 final List <Callable <Void >> tasks ;
203+ final ProgressBar [] pb = new ProgressBar [1 ];
200204 try (Stream <Path > stream = Files .walk (inRoot )) {
201205 tasks = stream .map (path -> (Callable <Void >) () -> {
202206 final String relative = GeneralUtil .slashName (inRoot .relativize (path ));
203207 final Path inOther = outRoot .resolve (relative );
204208 if (Files .isDirectory (path )) {
205209 Files .createDirectories (inOther );
210+ pb [0 ].step ();
206211 return null ;
207212 }
208213 final Path parent = inOther .getParent ();
@@ -211,27 +216,34 @@ private static void doConversion(
211216 }
212217 if (!relative .endsWith (".class" )) {
213218 Files .copy (path , inOther );
219+ pb [0 ].step ();
214220 return null ;
215221 }
216222 final String className = GeneralUtil .toClassName (relative );
217223 final byte [] bytecode = Files .readAllBytes (path );
218224 final byte [] result = transformerManager .transform (className , bytecode );
219225 Files .write (inOther , result != null ? result : bytecode );
220226
227+ pb [0 ].step ();
221228 return null ;
222229 }).collect (Collectors .toList ());
223230 }
224- final List <Future <Void >> futures = tasks .stream ().map (threadPool ::submit ).collect (Collectors .toList ());
225- threadPool .shutdown ();
226- for (Future <Void > future : ProgressBar .wrap (futures , new ProgressBarBuilder ().setTaskName ("Downgrading" ).setStyle (ProgressBarStyle .ASCII ))) {
227- try {
228- future .get ();
229- } catch (ExecutionException e ) {
230- throw e .getCause ();
231+ try {
232+ pb [0 ] = new ProgressBarBuilder ()
233+ .setTaskName ("Downgrading" )
234+ .setStyle (ProgressBarStyle .ASCII )
235+ .setInitialMax (tasks .size ())
236+ .setUpdateIntervalMillis (100 )
237+ .build ();
238+ threadPool .invokeAll (tasks );
239+ } finally {
240+ if (pb [0 ] != null ) {
241+ pb [0 ].close ();
231242 }
232243 }
233- if (!threadPool .awaitTermination (50 , TimeUnit .MILLISECONDS )) {
234- throw new IllegalStateException ("get() calls should have terminated thread pool" );
244+ threadPool .shutdown ();
245+ if (!threadPool .awaitTermination (1000 , TimeUnit .MILLISECONDS )) {
246+ throw new IllegalStateException ("Thread pool didn't shutdown correctly" );
235247 }
236248 LOGGER .info ("Writing final JAR" );
237249 }
0 commit comments