* optimize executors

This commit is contained in:
Reinhard Pointner 2014-11-06 13:52:16 +00:00
parent f65821f316
commit 80e76ed541
3 changed files with 31 additions and 66 deletions

View File

@ -128,6 +128,8 @@ public final class WebServices {
return null; return null;
} }
public static final ExecutorService requestThreadPool = Executors.newCachedThreadPool();
public static class TheTVDBClientWithLocalSearch extends TheTVDBClient { public static class TheTVDBClientWithLocalSearch extends TheTVDBClient {
public TheTVDBClientWithLocalSearch(String apikey) { public TheTVDBClientWithLocalSearch(String apikey) {
@ -166,48 +168,23 @@ public final class WebServices {
return null; return null;
} }
@SuppressWarnings("unchecked")
@Override @Override
public List<SearchResult> fetchSearchResult(final String query, final Locale locale) throws Exception { 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 Set<SearchResult> results = new LinkedHashSet<SearchResult>();
public List<SearchResult> call() throws Exception { for (Future<List<SearchResult>> resultSet : requestThreadPool.invokeAll(asList(localSearch, apiSearch))) {
return TheTVDBClientWithLocalSearch.super.fetchSearchResult(query, locale); try {
} results.addAll(resultSet.get());
}; } catch (ExecutionException e) {
Callable<List<SearchResult>> localSearch = new Callable<List<SearchResult>>() { if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause(); // unwrap cause
@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))) {
try {
results.addAll(resultSet.get());
} catch (ExecutionException e) {
if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause(); // unwrap cause
}
} }
} }
return sortBySimilarity(results, singleton(query), getSeriesMatchMetric(), false);
} finally {
executor.shutdownNow();
} }
}; return sortBySimilarity(results, singleton(query), getSeriesMatchMetric(), false);
}
} }
public static class AnidbClientWithLocalSearch extends AnidbClient { public static class AnidbClientWithLocalSearch extends AnidbClient {
@ -238,22 +215,17 @@ public final class WebServices {
Callable<List<? extends SearchResult>> seriesSearch = () -> seriesIndex.search(query, Locale.ENGLISH); Callable<List<? extends SearchResult>> seriesSearch = () -> seriesIndex.search(query, Locale.ENGLISH);
Callable<List<? extends SearchResult>> movieSearch = () -> movieIndex.searchMovie(query, Locale.ENGLISH); Callable<List<? extends SearchResult>> movieSearch = () -> movieIndex.searchMovie(query, Locale.ENGLISH);
ExecutorService executor = Executors.newFixedThreadPool(2); Set<SearchResult> results = new LinkedHashSet<SearchResult>();
try { for (Future<List<? extends SearchResult>> resultSet : requestThreadPool.invokeAll(asList(seriesSearch, movieSearch))) {
Set<SearchResult> results = new LinkedHashSet<SearchResult>(); try {
for (Future<List<? extends SearchResult>> resultSet : executor.invokeAll(asList(seriesSearch, movieSearch))) { results.addAll(resultSet.get());
try { } catch (ExecutionException e) {
results.addAll(resultSet.get()); if (e.getCause() instanceof Exception) {
} catch (ExecutionException e) { throw (Exception) e.getCause(); // unwrap cause
if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause(); // unwrap cause
}
} }
} }
return sortBySimilarity(results, singleton(query), new MetricAvg(getSeriesMatchMetric(), getMovieMatchMetric()), false);
} finally {
executor.shutdownNow();
} }
return sortBySimilarity(results, singleton(query), new MetricAvg(getSeriesMatchMetric(), getMovieMatchMetric()), false);
} }
@Override @Override

View File

@ -3,6 +3,7 @@ package net.filebot.ui.rename;
import static java.util.Collections.*; import static java.util.Collections.*;
import static net.filebot.MediaTypes.*; import static net.filebot.MediaTypes.*;
import static net.filebot.Settings.*; import static net.filebot.Settings.*;
import static net.filebot.WebServices.*;
import static net.filebot.media.MediaDetection.*; import static net.filebot.media.MediaDetection.*;
import static net.filebot.util.FileUtilities.*; import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.StringUtilities.*; import static net.filebot.util.StringUtilities.*;
@ -150,23 +151,15 @@ class EpisodeListMatcher implements AutoCompleteMatcher {
}); });
} }
// fetch episode lists concurrently // fetch episode lists concurrently and merge all episodes
ExecutorService executor = Executors.newCachedThreadPool(); Set<Episode> episodes = new LinkedHashSet<Episode>();
try { for (Future<List<Episode>> future : requestThreadPool.invokeAll(tasks)) {
// merge all episodes episodes.addAll(future.get());
Set<Episode> episodes = new LinkedHashSet<Episode>();
for (Future<List<Episode>> future : executor.invokeAll(tasks)) {
episodes.addAll(future.get());
}
// all background workers have finished
return episodes;
} finally {
// destroy background threads
executor.shutdownNow();
} }
// all background workers have finished
return episodes;
} }
@Override @Override

View File

@ -72,7 +72,7 @@ public class SubtitleUploadDialog extends JDialog {
private final OpenSubtitlesClient database; private final OpenSubtitlesClient database;
private ExecutorService checkExecutorService = Executors.newFixedThreadPool(2); private ExecutorService checkExecutorService = Executors.newSingleThreadExecutor();
private ExecutorService uploadExecutorService; private ExecutorService uploadExecutorService;
public SubtitleUploadDialog(OpenSubtitlesClient database, Window owner) { public SubtitleUploadDialog(OpenSubtitlesClient database, Window owner) {
@ -282,7 +282,7 @@ public class SubtitleUploadDialog extends JDialog {
return; return;
} }
uploadExecutorService = Executors.newFixedThreadPool(1); uploadExecutorService = Executors.newSingleThreadExecutor();
SubtitleMapping[] data = ((SubtitleMappingTableModel) subtitleMappingTable.getModel()).getData(); SubtitleMapping[] data = ((SubtitleMappingTableModel) subtitleMappingTable.getModel()).getData();
for (final SubtitleMapping it : data) { for (final SubtitleMapping it : data) {