* 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;
}
public static final ExecutorService requestThreadPool = Executors.newCachedThreadPool();
public static class TheTVDBClientWithLocalSearch extends TheTVDBClient {
public TheTVDBClientWithLocalSearch(String apikey) {
@ -166,48 +168,23 @@ 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))) {
try {
results.addAll(resultSet.get());
} catch (ExecutionException e) {
if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause(); // unwrap cause
}
Set<SearchResult> results = new LinkedHashSet<SearchResult>();
for (Future<List<SearchResult>> resultSet : requestThreadPool.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 {
@ -238,22 +215,17 @@ 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))) {
try {
results.addAll(resultSet.get());
} catch (ExecutionException e) {
if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause(); // unwrap cause
}
Set<SearchResult> results = new LinkedHashSet<SearchResult>();
for (Future<List<? extends SearchResult>> resultSet : requestThreadPool.invokeAll(asList(seriesSearch, movieSearch))) {
try {
results.addAll(resultSet.get());
} catch (ExecutionException e) {
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

View File

@ -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();
// fetch episode lists concurrently and merge all episodes
Set<Episode> episodes = new LinkedHashSet<Episode>();
try {
// merge all episodes
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();
for (Future<List<Episode>> future : requestThreadPool.invokeAll(tasks)) {
episodes.addAll(future.get());
}
// all background workers have finished
return episodes;
}
@Override

View File

@ -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) {