com.weloe.downloader.util.ConsoleProgressBar Maven / Gradle / Ivy
package com.weloe.downloader.util;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
*
* 进度条
* @author weloe
*/
public class ConsoleProgressBar {
private final int totalTasks;
private final AtomicInteger completedTasks;
private final Lock lock;
private final Condition updateCondition;
private Thread updateLabelThread;
private boolean finish = false;
private int preProgress;
public ConsoleProgressBar(int totalTasks) {
this.totalTasks = totalTasks;
this.completedTasks = new AtomicInteger(0);
this.lock = new ReentrantLock();
this.updateCondition = lock.newCondition();
}
public ConsoleProgressBar(int totalTasks,int completedTasks) {
this.totalTasks = totalTasks;
this.completedTasks = new AtomicInteger(completedTasks);
this.lock = new ReentrantLock();
this.updateCondition = lock.newCondition();
}
public void run(){
updateLabelThread = new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
if (finish) {
return;
}
updateProgressBar();
}
});
updateLabelThread.start();
}
public void interrupt(){
updateLabelThread.interrupt();
}
public void waitComplete() throws InterruptedException {
if (updateLabelThread != null) {
updateLabelThread.join();
}
}
public static void main(String[] args) throws InterruptedException {
ConsoleProgressBar consoleProgressBar = new ConsoleProgressBar(100);
Thread addTaskThread = new Thread(() -> {
for (int i = 0; i < consoleProgressBar.totalTasks; i++) {
consoleProgressBar.add();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
addTaskThread.start();
consoleProgressBar.run();
addTaskThread.join();
consoleProgressBar.waitComplete();
}
public void add() {
completedTasks.incrementAndGet();
notifyUpdate();
}
/**
* 通知更新线程进行更新
*/
private void notifyUpdate() {
lock.lock();
int progress = calProgress();
if (preProgress == progress) {
lock.unlock();
return;
}
updateCondition.signal();
lock.unlock();
}
private int calProgress() {
return (int) (completedTasks.get() / (double) totalTasks * 100);
}
public void updateProgressBar() {
lock.lock();
if (finish) {
return;
}
try {
updateCondition.await();
int progress = calProgress();
String progressBar = formatString(progress);
System.out.print(progressBar);
preProgress = progress;
if (progress >= 100) {
System.out.println();
finish = true;
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
lock.unlock();
}
}
public void print(){
int progress = calProgress();
String progressBar = formatString(progress);
System.out.print(progressBar);
}
public static String formatString(int progress) {
StringBuilder progressBar = new StringBuilder();
progressBar.append("[");
int numOfEquals = progress / 2;
for (int i = 0; i < numOfEquals; i++) {
progressBar.append("=");
}
progressBar.append(">");
for (int i = numOfEquals + 1; i <= 50; i++) {
progressBar.append(" ");
}
progressBar.append("] ");
progressBar.append(progress).append("%");
return "\r" + progressBar;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy