From 3a061ae3be1fbc243decea3c8a07c06c88da4ba0 Mon Sep 17 00:00:00 2001 From: Anthony Calosa Date: Tue, 12 Jul 2022 11:22:40 +0800 Subject: [PATCH] update ImageFetcher and setlookup - use newWorkStealingPool - update setlookup method --- forge-core/src/main/java/forge/ImageKeys.java | 41 +++++++++---------- .../src/main/java/forge/util/ThreadUtil.java | 21 ++++++++++ .../main/java/forge/util/ImageFetcher.java | 10 +++-- 3 files changed, 48 insertions(+), 24 deletions(-) diff --git a/forge-core/src/main/java/forge/ImageKeys.java b/forge-core/src/main/java/forge/ImageKeys.java index a1ac75b988b..7511bcf0604 100644 --- a/forge-core/src/main/java/forge/ImageKeys.java +++ b/forge-core/src/main/java/forge/ImageKeys.java @@ -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 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; } } } diff --git a/forge-core/src/main/java/forge/util/ThreadUtil.java b/forge-core/src/main/java/forge/util/ThreadUtil.java index 87e54edbc4c..c481816ff95 100644 --- a/forge-core/src/main/java/forge/util/ThreadUtil.java +++ b/forge-core/src/main/java/forge/util/ThreadUtil.java @@ -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 limit(Callable task, long millis){ + Future 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 executeWithTimeout(Callable task, int milliseconds) { ExecutorService executor = Executors.newCachedThreadPool(); Future future = executor.submit(task); diff --git a/forge-gui/src/main/java/forge/util/ImageFetcher.java b/forge-gui/src/main/java/forge/util/ImageFetcher.java index f0f513f201f..da702c2c5da 100644 --- a/forge-gui/src/main/java/forge/util/ImageFetcher.java +++ b/forge-gui/src/main/java/forge/util/ImageFetcher.java @@ -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 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);