Fixed task system
This commit is contained in:
@@ -88,6 +88,8 @@ public class ZipEngine {
|
||||
files = new File[1];
|
||||
files[0] = source;
|
||||
}
|
||||
if (files == null)
|
||||
return;
|
||||
String pathName;//存相对路径(相对于待压缩的根目录)
|
||||
for (File file : files)
|
||||
if (file.isDirectory()) {
|
||||
|
||||
@@ -17,28 +17,39 @@
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.util.tasks;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class DoubleTask extends Task {
|
||||
public class DoubleTask extends TaskInfo {
|
||||
|
||||
Task a, b;
|
||||
|
||||
public DoubleTask(Task a, Task b) {
|
||||
this(a, b, "Double Task");
|
||||
}
|
||||
|
||||
public DoubleTask(Task a, Task b, String info) {
|
||||
super(info);
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void executeTask() throws Throwable {
|
||||
a.executeTask();
|
||||
b.executeTask();
|
||||
public Collection<Task> getDependTasks() {
|
||||
return Arrays.asList(a);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInfo() {
|
||||
return "Double Task";
|
||||
public Collection<Task> getAfterTasks() {
|
||||
return Arrays.asList(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void executeTask() throws Throwable {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -101,6 +101,8 @@ public class TaskList extends Thread {
|
||||
AtomicBoolean bool = new AtomicBoolean(true);
|
||||
CountDownLatch counter = new CountDownLatch(c.size());
|
||||
for (Task t2 : c) {
|
||||
if (t2 == null)
|
||||
continue;
|
||||
t2.setParallelExecuting(true);
|
||||
Invoker thread = new Invoker(t2, counter, bool);
|
||||
invokers.add(thread);
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Hello Minecraft!.
|
||||
* Copyright (C) 2013 huangyuhui <huanghongxun2008@126.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see {http://www.gnu.org/licenses/}.
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.util.tasks;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import org.jackhuang.hellominecraft.util.tasks.communication.PreviousResult;
|
||||
import org.jackhuang.hellominecraft.util.tasks.communication.PreviousResultRegistrar;
|
||||
import org.jackhuang.hellominecraft.util.func.Consumer;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author huangyuhui
|
||||
* @param <T> Runnable<T>
|
||||
*/
|
||||
public class TaskRunnableArg1<T> extends TaskInfo implements PreviousResultRegistrar<T> {
|
||||
|
||||
private final Consumer<T> r;
|
||||
|
||||
public TaskRunnableArg1(String info, Consumer<T> r) {
|
||||
super(info);
|
||||
this.r = r;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void executeTask() throws Exception {
|
||||
if (al.size() != 1)
|
||||
throw new IllegalStateException("the count of args is not one.");
|
||||
r.accept(al.get(0).getResult());
|
||||
}
|
||||
|
||||
ArrayList<PreviousResult<T>> al = new ArrayList();
|
||||
|
||||
@Override
|
||||
public Task registerPreviousResult(PreviousResult<T> pr) {
|
||||
al.add(pr);
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -216,8 +216,8 @@ public class TaskWindow extends javax.swing.JDialog
|
||||
if (idx == -1)
|
||||
return;
|
||||
int pgs = progress * 100 / max;
|
||||
if (progresses.contains(idx) && progresses.get(idx) != pgs && lstDownload.getRowCount() > idx) {
|
||||
SwingUtils.setValueAt(lstDownload, pgs + "%", idx, 1);
|
||||
if (progresses.size() > idx && progresses.get(idx) != pgs && lstDownload.getRowCount() > idx) {
|
||||
SwingUtils.setValueAt(lstDownload, pgs < 0 ? "???" : pgs + "%", idx, 1);
|
||||
progresses.set(idx, pgs);
|
||||
}
|
||||
});
|
||||
@@ -242,7 +242,7 @@ public class TaskWindow extends javax.swing.JDialog
|
||||
if (taskList == null)
|
||||
return;
|
||||
tasks.add(task);
|
||||
progresses.add(0);
|
||||
progresses.add(-1);
|
||||
SwingUtils.appendLast(lstDownload, task.getInfo(), "0%");
|
||||
SwingUtils.moveEnd(srlDownload);
|
||||
});
|
||||
|
||||
@@ -130,6 +130,8 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
|
||||
if (!shouldContinue)
|
||||
break;
|
||||
try {
|
||||
if (ppl != null)
|
||||
ppl.setProgress(this, -1, 1);
|
||||
|
||||
// Open connection to URL.
|
||||
HttpURLConnection connection
|
||||
|
||||
@@ -57,15 +57,22 @@ public class HTTPGetTask extends TaskInfo implements PreviousResult<String> {
|
||||
if (repeat > 0)
|
||||
HMCLog.warn("Failed to download, repeat: " + repeat);
|
||||
try {
|
||||
if (ppl != null)
|
||||
ppl.setProgress(this, -1, 1);
|
||||
URLConnection conn = new URL(url).openConnection();
|
||||
InputStream is = conn.getInputStream();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
int i;
|
||||
int size = conn.getContentLength(), read = 0;
|
||||
long lastTime = System.currentTimeMillis();
|
||||
while ((i = is.read()) != -1) {
|
||||
baos.write(i);
|
||||
if (ppl != null)
|
||||
ppl.setProgress(this, ++read, size);
|
||||
++read;
|
||||
long now = System.currentTimeMillis();
|
||||
if (ppl != null && (now - lastTime) >= 1000) {
|
||||
ppl.setProgress(this, read, size);
|
||||
lastTime = now;
|
||||
}
|
||||
if (!shouldContinue)
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user