This commit is contained in:
huangyuhui
2016-02-24 14:10:15 +08:00
parent 52f761095a
commit 1bb7f8ac60
2 changed files with 12 additions and 19 deletions

View File

@@ -24,6 +24,8 @@ import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.jackhuang.hellominecraft.util.logging.HMCLog; import org.jackhuang.hellominecraft.util.logging.HMCLog;
/** /**
@@ -64,40 +66,36 @@ public class TaskList extends Thread {
return totTask; return totTask;
} }
private class InvokeThread extends Thread { private class Invoker implements Runnable {
Task task; Task task;
Set<InvokeThread> s; Set<Invoker> s;
public InvokeThread(Task task, Set<InvokeThread> ss) { public Invoker(Task task, Set<Invoker> ss) {
this.task = task; this.task = task;
s = ss; s = ss;
setDaemon(true);
} }
@Override @Override
public void run() { public void run() {
executeTask(task); executeTask(task);
s.remove(this); s.remove(this);
THREAD_POOL.remove(this);
} }
} }
static final Set<InvokeThread> THREAD_POOL = Collections.synchronizedSet(new HashSet<InvokeThread>()); static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(64);
static final Set<Task> TASK_POOL = Collections.synchronizedSet(new HashSet<Task>());
private void processTasks(Collection<? extends Task> c) { private void processTasks(Collection<? extends Task> c) {
if (c == null || c.isEmpty()) if (c == null || c.isEmpty())
return; return;
this.totTask += c.size(); this.totTask += c.size();
Set<InvokeThread> runningThread = Collections.synchronizedSet(new HashSet<InvokeThread>()); Set<Invoker> runningThread = Collections.synchronizedSet(new HashSet<Invoker>());
for (Task t2 : c) { for (Task t2 : c) {
t2.setParallelExecuting(true); t2.setParallelExecuting(true);
InvokeThread thread = new InvokeThread(t2, runningThread); Invoker thread = new Invoker(t2, runningThread);
THREAD_POOL.add(thread);
runningThread.add(thread); runningThread.add(thread);
thread.start(); EXECUTOR_SERVICE.execute(thread);
} }
while (!runningThread.isEmpty()) while (!runningThread.isEmpty())
try { try {
@@ -153,7 +151,6 @@ public class TaskList extends Thread {
public void run() { public void run() {
Thread.currentThread().setName("TaskList"); Thread.currentThread().setName("TaskList");
THREAD_POOL.clear();
totTask = taskQueue.size(); totTask = taskQueue.size();
while (!taskQueue.isEmpty()) while (!taskQueue.isEmpty())
executeTask(taskQueue.remove(0)); executeTask(taskQueue.remove(0));
@@ -168,13 +165,7 @@ public class TaskList extends Thread {
public void abort() { public void abort() {
shouldContinue = false; shouldContinue = false;
while (!THREAD_POOL.isEmpty()) EXECUTOR_SERVICE.shutdownNow();
synchronized (THREAD_POOL) {
InvokeThread it = THREAD_POOL.iterator().next();
if (!it.task.abort())
it.interrupt();
THREAD_POOL.remove(it);
}
this.interrupt(); this.interrupt();
} }

View File

@@ -154,6 +154,8 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
File tempFile = new File(filePath.getAbsolutePath() + ".hmd"); File tempFile = new File(filePath.getAbsolutePath() + ".hmd");
if (!tempFile.exists()) if (!tempFile.exists())
tempFile.createNewFile(); tempFile.createNewFile();
else if (!tempFile.renameTo(tempFile)) // check file lock
throw new RuntimeException("The temp file is locked, maybe there is an application using the file?");
// Open file and seek to the end of it. // Open file and seek to the end of it.
file = new RandomAccessFile(tempFile, "rw"); file = new RandomAccessFile(tempFile, "rw");