michael-o commented on code in PR #1302: URL: https://github.com/apache/maven/pull/1302#discussion_r1447204050
########## maven-embedder/src/main/java/org/apache/maven/cli/transfer/ConsoleMavenTransferListener.java: ########## @@ -34,79 +36,159 @@ */ public class ConsoleMavenTransferListener extends AbstractMavenTransferListener { - private Map<TransferResource, Long> transfers = new LinkedHashMap<>(); - private FileSizeFormat format = new FileSizeFormat(Locale.ENGLISH); // use in a synchronized fashion - private StringBuilder buffer = new StringBuilder(128); // use in a synchronized fashion + public static final int MIN_DELAY_BETWEEN_UPDATES = 50; + private final Map<TransferResource, TransferEvent> transfers = + new ConcurrentSkipListMap<>(Comparator.comparing(TransferResource::getResourceName)); + private final FileSizeFormat format = new FileSizeFormat(Locale.ENGLISH); + private final ThreadLocal<StringBuilder> buffers = ThreadLocal.withInitial(() -> new StringBuilder(128)); - private boolean printResourceNames; + private final boolean printResourceNames; private int lastLength; + private final Object lock = new Object(); + private final Thread updater; public ConsoleMavenTransferListener(PrintStream out, boolean printResourceNames) { super(out); this.printResourceNames = printResourceNames; + updater = new Thread(this::update); + updater.setDaemon(true); + updater.start(); } @Override - public synchronized void transferInitiated(TransferEvent event) { - overridePreviousTransfer(event); - - super.transferInitiated(event); + public void transferInitiated(TransferEvent event) { + transfers.put(event.getResource(), event); + synchronized (lock) { + lock.notifyAll(); + } } @Override - public synchronized void transferCorrupted(TransferEvent event) throws TransferCancelledException { - overridePreviousTransfer(event); - - super.transferCorrupted(event); + public void transferCorrupted(TransferEvent event) throws TransferCancelledException { + transfers.put(event.getResource(), event); + synchronized (lock) { + lock.notifyAll(); + } } @Override - public synchronized void transferProgressed(TransferEvent event) throws TransferCancelledException { - TransferResource resource = event.getResource(); - transfers.put(resource, event.getTransferredBytes()); - - buffer.append("Progress (").append(transfers.size()).append("): "); - - Iterator<Map.Entry<TransferResource, Long>> entries = - transfers.entrySet().iterator(); - while (entries.hasNext()) { - Map.Entry<TransferResource, Long> entry = entries.next(); - long total = entry.getKey().getContentLength(); - Long complete = entry.getValue(); + public void transferProgressed(TransferEvent event) throws TransferCancelledException { + transfers.put(event.getResource(), event); + synchronized (lock) { + lock.notifyAll(); + } + } - String resourceName = entry.getKey().getResourceName(); + @Override + public void transferSucceeded(TransferEvent event) { + transfers.put(event.getResource(), event); + synchronized (lock) { + if (transfers.size() == 1) { + doUpdate(); + } else { + lock.notifyAll(); + } + } + } - if (printResourceNames) { - int idx = resourceName.lastIndexOf('/'); + @Override + public void transferFailed(TransferEvent event) { + transfers.put(event.getResource(), event); + synchronized (lock) { + if (transfers.size() == 1) { + doUpdate(); + } else { + lock.notifyAll(); + } + } + } - if (idx < 0) { - buffer.append(resourceName); - } else { - buffer.append(resourceName, idx + 1, resourceName.length()); + void update() { + synchronized (lock) { + try { + long t0 = System.currentTimeMillis(); + while (true) { + lock.wait(100); + long t1 = System.currentTimeMillis(); + if (t1 - t0 > MIN_DELAY_BETWEEN_UPDATES) { Review Comment: I am not talking about resolution, but the fact that current millis is wall clock and can run backwards. `nanoTime()` is guaranteed to be at least monotonic. -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org