* optimize executors
This commit is contained in:
parent
f65821f316
commit
80e76ed541
|
@ -128,6 +128,8 @@ public final class WebServices {
|
|||
return null;
|
||||
}
|
||||
|
||||
public static final ExecutorService requestThreadPool = Executors.newCachedThreadPool();
|
||||
|
||||
public static class TheTVDBClientWithLocalSearch extends TheTVDBClient {
|
||||
|
||||
public TheTVDBClientWithLocalSearch(String apikey) {
|
||||
|
@ -166,35 +168,13 @@ public final class WebServices {
|
|||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public List<SearchResult> fetchSearchResult(final String query, final Locale locale) throws Exception {
|
||||
Callable<List<SearchResult>> apiSearch = new Callable<List<SearchResult>>() {
|
||||
Callable<List<SearchResult>> apiSearch = () -> TheTVDBClientWithLocalSearch.super.fetchSearchResult(query, locale);
|
||||
Callable<List<SearchResult>> localSearch = () -> getLocalIndex().search(query);
|
||||
|
||||
@Override
|
||||
public List<SearchResult> call() throws Exception {
|
||||
return TheTVDBClientWithLocalSearch.super.fetchSearchResult(query, locale);
|
||||
}
|
||||
};
|
||||
Callable<List<SearchResult>> localSearch = new Callable<List<SearchResult>>() {
|
||||
|
||||
@Override
|
||||
public List<SearchResult> call() throws Exception {
|
||||
try {
|
||||
return getLocalIndex().search(query);
|
||||
} catch (Exception e) {
|
||||
Logger.getLogger(TheTVDBClientWithLocalSearch.class.getName()).log(Level.SEVERE, e.getMessage(), e);
|
||||
}
|
||||
|
||||
// let local search fail gracefully without affecting API search
|
||||
return emptyList();
|
||||
}
|
||||
};
|
||||
|
||||
ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||
try {
|
||||
Set<SearchResult> results = new LinkedHashSet<SearchResult>();
|
||||
for (Future<List<SearchResult>> resultSet : executor.invokeAll(asList(localSearch, apiSearch))) {
|
||||
for (Future<List<SearchResult>> resultSet : requestThreadPool.invokeAll(asList(localSearch, apiSearch))) {
|
||||
try {
|
||||
results.addAll(resultSet.get());
|
||||
} catch (ExecutionException e) {
|
||||
|
@ -204,10 +184,7 @@ public final class WebServices {
|
|||
}
|
||||
}
|
||||
return sortBySimilarity(results, singleton(query), getSeriesMatchMetric(), false);
|
||||
} finally {
|
||||
executor.shutdownNow();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static class AnidbClientWithLocalSearch extends AnidbClient {
|
||||
|
@ -238,10 +215,8 @@ public final class WebServices {
|
|||
Callable<List<? extends SearchResult>> seriesSearch = () -> seriesIndex.search(query, Locale.ENGLISH);
|
||||
Callable<List<? extends SearchResult>> movieSearch = () -> movieIndex.searchMovie(query, Locale.ENGLISH);
|
||||
|
||||
ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||
try {
|
||||
Set<SearchResult> results = new LinkedHashSet<SearchResult>();
|
||||
for (Future<List<? extends SearchResult>> resultSet : executor.invokeAll(asList(seriesSearch, movieSearch))) {
|
||||
for (Future<List<? extends SearchResult>> resultSet : requestThreadPool.invokeAll(asList(seriesSearch, movieSearch))) {
|
||||
try {
|
||||
results.addAll(resultSet.get());
|
||||
} catch (ExecutionException e) {
|
||||
|
@ -251,9 +226,6 @@ public final class WebServices {
|
|||
}
|
||||
}
|
||||
return sortBySimilarity(results, singleton(query), new MetricAvg(getSeriesMatchMetric(), getMovieMatchMetric()), false);
|
||||
} finally {
|
||||
executor.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -3,6 +3,7 @@ package net.filebot.ui.rename;
|
|||
import static java.util.Collections.*;
|
||||
import static net.filebot.MediaTypes.*;
|
||||
import static net.filebot.Settings.*;
|
||||
import static net.filebot.WebServices.*;
|
||||
import static net.filebot.media.MediaDetection.*;
|
||||
import static net.filebot.util.FileUtilities.*;
|
||||
import static net.filebot.util.StringUtilities.*;
|
||||
|
@ -150,23 +151,15 @@ class EpisodeListMatcher implements AutoCompleteMatcher {
|
|||
});
|
||||
}
|
||||
|
||||
// fetch episode lists concurrently
|
||||
ExecutorService executor = Executors.newCachedThreadPool();
|
||||
|
||||
try {
|
||||
// merge all episodes
|
||||
// fetch episode lists concurrently and merge all episodes
|
||||
Set<Episode> episodes = new LinkedHashSet<Episode>();
|
||||
|
||||
for (Future<List<Episode>> future : executor.invokeAll(tasks)) {
|
||||
for (Future<List<Episode>> future : requestThreadPool.invokeAll(tasks)) {
|
||||
episodes.addAll(future.get());
|
||||
}
|
||||
|
||||
// all background workers have finished
|
||||
return episodes;
|
||||
} finally {
|
||||
// destroy background threads
|
||||
executor.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -72,7 +72,7 @@ public class SubtitleUploadDialog extends JDialog {
|
|||
|
||||
private final OpenSubtitlesClient database;
|
||||
|
||||
private ExecutorService checkExecutorService = Executors.newFixedThreadPool(2);
|
||||
private ExecutorService checkExecutorService = Executors.newSingleThreadExecutor();
|
||||
private ExecutorService uploadExecutorService;
|
||||
|
||||
public SubtitleUploadDialog(OpenSubtitlesClient database, Window owner) {
|
||||
|
@ -282,7 +282,7 @@ public class SubtitleUploadDialog extends JDialog {
|
|||
return;
|
||||
}
|
||||
|
||||
uploadExecutorService = Executors.newFixedThreadPool(1);
|
||||
uploadExecutorService = Executors.newSingleThreadExecutor();
|
||||
|
||||
SubtitleMapping[] data = ((SubtitleMappingTableModel) subtitleMappingTable.getModel()).getData();
|
||||
for (final SubtitleMapping it : data) {
|
||||
|
|
Loading…
Reference in New Issue