update ImageFetcher and setlookup

- use newWorkStealingPool
- update setlookup method
This commit is contained in:
Anthony Calosa
2022-07-12 11:22:40 +08:00
parent b664acc3fc
commit 3a061ae3be
3 changed files with 48 additions and 24 deletions

View File

@@ -173,16 +173,12 @@ public final class ImageKeys {
}
//setlookup
if (hasSetLookup(filename)) {
//delay processing so gui is responsive
ThreadUtil.delay(60, new Runnable() {
@Override
public void run() {
File f = setLookUpFile(filename, fullborderFile);
if (f != null)
cachedCards.put(filename, f);
else //is null
missingCards.add(filename);
}
ThreadUtil.getServicePool().submit(() -> {
File f = setLookUpFile(filename, fullborderFile);
if (f != null)
cachedCards.put(filename, f);
else //is null
missingCards.add(filename);
});
}
}
@@ -283,27 +279,30 @@ public final class ImageKeys {
for (String setLookup : StaticData.instance().getSetLookup().get(setKey)) {
String lookupDirectory = CACHE_CARD_PICS_DIR + setLookup;
File f = new File(lookupDirectory);
String[] cardNames = f.list();
if (cardNames != null) {
Set<String> cardList = new HashSet<>(Arrays.asList(cardNames));
if (f.exists() && f.isDirectory()) {
for (String ext : FILE_EXTENSIONS) {
if (ext.equals(""))
continue;
File placeholder;
String fb1 = fullborderFile.replace(setKey+"/","")+ext;
if (cardList.contains(fb1)) {
return new File(lookupDirectory+"/"+fb1);
placeholder = new File(lookupDirectory+"/"+fb1);
if (placeholder.exists()) {
return placeholder;
}
String fb2 = fullborderFile.replace(setKey+"/","").replaceAll("[0-9]*.fullborder", "1.fullborder")+ext;
if (cardList.contains(fb2)) {
return new File(lookupDirectory+"/"+fb2);
placeholder = new File(lookupDirectory+"/"+fb2);
if (placeholder.exists()) {
return placeholder;
}
String f1 = filename.replace(setKey+"/","")+ext;
if (cardList.contains(f1)) {
return new File(lookupDirectory+"/"+f1);
placeholder = new File(lookupDirectory+"/"+f1);
if (placeholder.exists()) {
return placeholder;
}
String f2 = filename.replace(setKey+"/","").replaceAll("[0-9]*.full", "1.full")+ext;
if (cardList.contains(f2)) {
return new File(lookupDirectory+"/"+f2);
placeholder = new File(lookupDirectory+"/"+f2);
if (placeholder.exists()) {
return placeholder;
}
}
}

View File

@@ -53,6 +53,27 @@ public class ThreadUtil {
return Thread.currentThread().getName().startsWith("Game");
}
private static ExecutorService service = Executors.newWorkStealingPool();
public static ExecutorService getServicePool() {
return service;
}
public static void refreshServicePool() {
service = Executors.newWorkStealingPool();
}
public static <T> T limit(Callable<T> task, long millis){
Future<T> future = null;
T result;
try {
future = service.submit(task);
result = future.get(millis, TimeUnit.MILLISECONDS);
} catch (Exception e) {
result = null;
} finally {
if (future != null)
future.cancel(true);
}
return result;
}
public static <T> T executeWithTimeout(Callable<T> task, int milliseconds) {
ExecutorService executor = Executors.newCachedThreadPool();
Future<T> future = executor.submit(task);

View File

@@ -3,7 +3,7 @@ package forge.util;
import java.io.File;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import forge.card.CardEdition;
import forge.item.IPaperCard;
@@ -18,7 +18,7 @@ import forge.localinstance.properties.ForgePreferences;
import forge.model.FModel;
public abstract class ImageFetcher {
private static final ExecutorService threadPool = Executors.newCachedThreadPool();
private static final ExecutorService threadPool = ThreadUtil.getServicePool();
// see https://scryfall.com/docs/api/languages and
// https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
private static final HashMap<String, String> langCodeMap = new HashMap<>();
@@ -167,7 +167,11 @@ public abstract class ImageFetcher {
currentFetches.remove(destPath);
}
};
threadPool.submit(getDownloadTask(downloadUrls.toArray(new String[0]), destPath, notifyObservers));
try {
threadPool.submit(getDownloadTask(downloadUrls.toArray(new String[0]), destPath, notifyObservers));
} catch (RejectedExecutionException re) {
re.printStackTrace();
}
}
protected abstract Runnable getDownloadTask(String[] toArray, String destPath, Runnable notifyObservers);