* 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,35 +168,13 @@ 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
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>(); 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 { try {
results.addAll(resultSet.get()); results.addAll(resultSet.get());
} catch (ExecutionException e) { } catch (ExecutionException e) {
@ -204,10 +184,7 @@ public final class WebServices {
} }
} }
return sortBySimilarity(results, singleton(query), getSeriesMatchMetric(), false); return sortBySimilarity(results, singleton(query), getSeriesMatchMetric(), false);
} finally {
executor.shutdownNow();
} }
};
} }
public static class AnidbClientWithLocalSearch extends AnidbClient { 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>> 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);
try {
Set<SearchResult> results = new LinkedHashSet<SearchResult>(); 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 { try {
results.addAll(resultSet.get()); results.addAll(resultSet.get());
} catch (ExecutionException e) { } catch (ExecutionException e) {
@ -251,9 +226,6 @@ public final class WebServices {
} }
} }
return sortBySimilarity(results, singleton(query), new MetricAvg(getSeriesMatchMetric(), getMovieMatchMetric()), false); return sortBySimilarity(results, singleton(query), new MetricAvg(getSeriesMatchMetric(), getMovieMatchMetric()), false);
} finally {
executor.shutdownNow();
}
} }
@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();
try {
// merge all episodes
Set<Episode> episodes = new LinkedHashSet<Episode>(); 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()); episodes.addAll(future.get());
} }
// all background workers have finished // all background workers have finished
return episodes; return episodes;
} finally {
// destroy background threads
executor.shutdownNow();
}
} }
@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) {