本文整理汇总了Java中java.util.concurrent.ExecutorCompletionService.submit方法的典型用法代码示例。如果您正苦于以下问题:Java ExecutorCompletionService.submit方法的具体用法?Java ExecutorCompletionService.submit怎么用?Java ExecutorCompletionService.submit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ExecutorCompletionService
的用法示例。
在下文中一共展示了ExecutorCompletionService.submit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testMultipleClients
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
@Test
public void testMultipleClients() throws Exception {
ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS);
try {
ExecutorCompletionService<Boolean> ecs =
new ExecutorCompletionService<Boolean>(exec);
for (int i = 0; i < NUM_THREADS; ++i)
ecs.submit(new IdLockTestThread("client_" + i));
for (int i = 0; i < NUM_THREADS; ++i) {
Future<Boolean> result = ecs.take();
assertTrue(result.get());
}
idLock.assertMapEmpty();
} finally {
exec.shutdown();
exec.awaitTermination(5000, TimeUnit.MILLISECONDS);
}
}
示例2: putEvents
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
private void putEvents(final KafkaChannel channel, final List<List<Event>>
events, ExecutorCompletionService<Void> submitterSvc) {
for (int i = 0; i < 5; i++) {
final int index = i;
submitterSvc.submit(new Callable<Void>() {
@Override
public Void call() {
Transaction tx = channel.getTransaction();
tx.begin();
List<Event> eventsToPut = events.get(index);
for (int j = 0; j < 10; j++) {
channel.put(eventsToPut.get(j));
}
try {
tx.commit();
} finally {
tx.close();
}
return null;
}
});
}
}
示例3: testMultipleClients
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
@Test(timeout = 60000)
public void testMultipleClients() throws Exception {
ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS);
try {
ExecutorCompletionService<Boolean> ecs =
new ExecutorCompletionService<Boolean>(exec);
for (int i = 0; i < NUM_THREADS; ++i)
ecs.submit(new IdLockTestThread("client_" + i));
for (int i = 0; i < NUM_THREADS; ++i) {
Future<Boolean> result = ecs.take();
assertTrue(result.get());
}
// make sure the entry pool will be cleared after GC and purge call
int entryPoolSize = idLock.purgeAndGetEntryPoolSize();
LOG.debug("Size of entry pool after gc and purge: " + entryPoolSize);
assertEquals(0, entryPoolSize);
} finally {
exec.shutdown();
exec.awaitTermination(5000, TimeUnit.MILLISECONDS);
}
}
示例4: loadTracksAsynchronously
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
private AudioPlaylist loadTracksAsynchronously(List<String> videoIds, String selectedVideoId) {
ExecutorCompletionService<AudioItem> completion = new ExecutorCompletionService<>(mixLoadingExecutor);
List<AudioTrack> tracks = new ArrayList<>();
for (final String videoId : videoIds) {
completion.submit(() -> sourceManager.loadTrackWithVideoId(videoId, true));
}
try {
fetchTrackResultsFromExecutor(completion, tracks, videoIds.size());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
AudioTrack selectedTrack = sourceManager.findSelectedTrack(tracks, selectedVideoId);
if (tracks.isEmpty()) {
throw new FriendlyException("No tracks from the mix loaded succesfully.", SUSPICIOUS, null);
} else if (selectedTrack == null) {
throw new FriendlyException("The selected track of the mix failed to load.", SUSPICIOUS, null);
}
return new BasicAudioPlaylist("YouTube mix", tracks, selectedTrack, false);
}
示例5: testMultiTopology
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
/**
* Submit multiple jobs concurrently using ProcessSource.
*/
@Test
public void testMultiTopology() throws Exception {
int executions = 4;
ExecutorCompletionService<Boolean> completer = new ExecutorCompletionService<>(
Executors.newFixedThreadPool(executions));
for (int i = 0; i < executions; i++) {
completer.submit(() -> {
Topology t = newTopology();
TStream<String> s = t.strings("a", "b", "c", "d", "e", "f", "g", "h");
s.sink((tuple) -> { if ("h".equals(tuple)) System.out.println(tuple);});
Condition<Long> tc = t.getTester().tupleCount(s, 8);
complete(t, tc);
return true;
});
}
waitForCompletion(completer, executions);
}
示例6: testMultiTopologyWithError
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
/**
* Submit multiple jobs concurrently using ProcessSource.
*/
@Test
public void testMultiTopologyWithError() throws Exception {
int executions = 4;
ExecutorCompletionService<Boolean> completer = new ExecutorCompletionService<>(
Executors.newFixedThreadPool(executions));
for (int i = 0; i < executions; i++) {
completer.submit(() -> {
Topology t = newTopology();
TStream<String> s = t.strings("a", "b", "c", "d", "e", "f", "g", "h");
// Throw on the 8th tuple
s.sink((tuple) -> { if ("h".equals(tuple)) throw new RuntimeException("Expected Test Exception");});
// Expect 7 tuples out of 8
Condition<Long> tc = t.getTester().tupleCount(s, 7);
complete(t, tc);
return true;
});
}
waitForCompletion(completer, executions);
}
示例7: testMultiTopologyPollWithError
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
/**
* Submit multiple jobs concurrently using PeriodicSource.
*/
@Test
public void testMultiTopologyPollWithError() throws Exception {
int executions = 4;
ExecutorCompletionService<Boolean> completer = new ExecutorCompletionService<>(
Executors.newFixedThreadPool(executions));
for (int i = 0; i < executions; i++) {
completer.submit(() -> {
Topology t = newTopology();
AtomicLong n = new AtomicLong(0);
TStream<Long> s = t.poll(() -> n.incrementAndGet(), 10, TimeUnit.MILLISECONDS);
// Throw on the 8th tuple
s.sink((tuple) -> { if (8 == n.get()) throw new RuntimeException("Expected Test Exception");});
// Expect 7 tuples out of 8
Condition<Long> tc = t.getTester().tupleCount(s, 7);
complete(t, tc);
return true;
});
}
waitForCompletion(completer, executions);
}
示例8: testMultipleClients
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
@Test
public void testMultipleClients() throws Exception {
ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS);
try {
ExecutorCompletionService<Boolean> ecs = new ExecutorCompletionService<>(exec);
for (int i = 0; i < NUM_THREADS; ++i)
ecs.submit(new IdLockTestThread("client_" + i));
for (int i = 0; i < NUM_THREADS; ++i) {
Future<Boolean> result = ecs.take();
assertTrue(result.get());
}
idLock.assertMapEmpty();
} finally {
exec.shutdown();
exec.awaitTermination(5000, TimeUnit.MILLISECONDS);
}
}
示例9: positiveTest
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
@Test
public void positiveTest() {
// create the threads manager
this.threadsManager = new ThreadsManager();
// create all the threads
ThreadPoolExecutor executor = ( ThreadPoolExecutor ) Executors.newCachedThreadPool();
executor.setKeepAliveTime( 0, TimeUnit.SECONDS );
ExecutorCompletionService<Object> executionService = new ExecutorCompletionService<Object>( executor );
IterationListener listener = new IterationListener();
msg( log, "Ask Java to create all threads" );
for( int j = 0; j < N_THREADS; j++ ) {
executionService.submit( new TestWorker( N_ITERATIONS, threadsManager, listener ), null );
}
// run all iterations
for( int i = 0; i < N_ITERATIONS; i++ ) {
msg( log, "ITERATION " + i + "\n\n" );
runThisIteration();
waitForIterationCompletion();
}
// it may take a little while all threads are gone
try {
Thread.sleep( 1000 );
} catch( InterruptedException e ) {}
// check if there are any remaining threads started by this test
checkAllRemainingThreads();
}
示例10: searchPointers
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
public void searchPointers() throws Exception
{
memoryPointers.clear();
MemoryDump memoryDump = memoryDumps.get(0);
setPointerAddressRange(memoryDump);
// Use a modified amount of threads because it works best
Runtime runtime = Runtime.getRuntime();
int threadsCount = runtime.availableProcessors() * 2;
ByteBufferRange[] byteBufferRanges = getByteBufferRanges(memoryDump, threadsCount);
ExecutorService executorService = Executors.newFixedThreadPool(threadsCount);
ExecutorCompletionService<String> completionService = new ExecutorCompletionService<>(executorService);
long targetAddress = memoryDump.getTargetAddress();
// Process each range concurrently
for (ByteBufferRange byteBufferRange : byteBufferRanges)
{
Callable<String> searchTask = () ->
{
searchPointers(targetAddress, byteBufferRange);
return null;
};
// Start the search
completionService.submit(searchTask);
}
// Wait for all tasks to finish
for (ByteBufferRange ignored : byteBufferRanges)
{
completionService.take().get();
}
// Shutdown the thread pool
executorService.shutdown();
}
示例11: testConcurrentReading
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
@Test
public void testConcurrentReading() throws Exception {
for (Compression.Algorithm compressAlgo : COMPRESSION_ALGORITHMS) {
Path path =
new Path(TEST_UTIL.getDataTestDir(), "concurrent_reading");
Random rand = defaultRandom();
List<Long> offsets = new ArrayList<Long>();
List<BlockType> types = new ArrayList<BlockType>();
writeBlocks(rand, compressAlgo, path, offsets, null, types, null);
FSDataInputStream is = fs.open(path);
long fileSize = fs.getFileStatus(path).getLen();
HFileBlock.FSReader hbr = new HFileBlock.FSReaderV2(is, compressAlgo,
fileSize);
Executor exec = Executors.newFixedThreadPool(NUM_READER_THREADS);
ExecutorCompletionService<Boolean> ecs =
new ExecutorCompletionService<Boolean>(exec);
for (int i = 0; i < NUM_READER_THREADS; ++i) {
ecs.submit(new BlockReaderThread("reader_" + (char) ('A' + i), hbr,
offsets, types, fileSize));
}
for (int i = 0; i < NUM_READER_THREADS; ++i) {
Future<Boolean> result = ecs.take();
assertTrue(result.get());
if (detailedLogging) {
LOG.info(String.valueOf(i + 1)
+ " reader threads finished successfully (algo=" + compressAlgo
+ ")");
}
}
is.close();
}
}
示例12: execute
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
private void execute(String[] args) throws IOException {
if (args.length != 3) {
usage();
System.exit(1);
}
if (args[0].contains("-h")) {
usage();
System.exit(0);
}
int numThreads = Integer.parseInt(args[0]);
ArrayBlockingQueue<Path> queue = new ArrayBlockingQueue<>(1000);
rootDir = Paths.get(args[1]);
QueueFiller filler = new QueueFiller(rootDir, queue, numThreads);
new Thread(filler).start();
outputDir = Paths.get(args[2]);
Files.createDirectories(outputDir);
ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
ExecutorCompletionService<Integer> executorCompletionService = new ExecutorCompletionService<Integer>(executorService);
for (int i = 0; i < numThreads; i++) {
executorCompletionService.submit(new FileRunner(queue,
Files.newBufferedWriter(outputDir.resolve("mimes-"+i+".txt"))));
}
int completed = 0;
while (completed < numThreads) {
try {
Future<Integer> future = executorCompletionService.poll(1, TimeUnit.SECONDS);
if (future != null) {
completed++;
}
} catch (InterruptedException e) {
}
}
executorService.shutdown();
executorService.shutdownNow();
System.exit(0);
}
示例13: run
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
public void run() {
// Prepare executor
ExecutorService executor =
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
ExecutorCompletionService<List<VocabularyWord>> completionService =
new ExecutorCompletionService<>(executor);
// Split words into groups of predefined size
// We do this in order to mimic normal user behavior (and because there is a limit to how long URL can be)
List<List<VocabularyWord>> groups =
Stream.of(words).slidingWindow(WORDS_PER_GROUP, WORDS_PER_GROUP).collect(toList());
// Submit them to executor
for (List<VocabularyWord> group : groups) {
completionService.submit(() -> translate(group));
}
List<VocabularyWord> wordsList = new ArrayList<>();
// Collect results
for (int i = 0; i < groups.size(); i++) {
try {
wordsList.addAll(completionService.take().get());
} catch (InterruptedException | ExecutionException e) {
logger.error("Failed to translated group of words", e);
}
}
// Sort them by normalized words, otherwise all letters with diacritics would be placed at the end
Collections.sort(wordsList, (o1, o2) -> o1.getNormalizedWord().compareTo(o2.getNormalizedWord()));
observer.onNext(wordsList);
provider.putWords(wordsList);
logger.debug("LoadWordsTask run() returned list of size: [{}]", wordsList.size());
}
示例14: recoverFromLog
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
public Future<RecoveryInfo> recoverFromLog() {
recoveryInfo = new RecoveryInfo();
List<TransactionLog> recoverLogs = new ArrayList<>(1);
for (TransactionLog ll : newestLogsOnStartup) {
if (!ll.try_incref()) continue;
try {
if (ll.endsWithCommit()) {
ll.decref();
continue;
}
} catch (IOException e) {
log.error("Error inspecting tlog " + ll, e);
ll.decref();
continue;
}
recoverLogs.add(ll);
}
if (recoverLogs.isEmpty()) return null;
ExecutorCompletionService<RecoveryInfo> cs = new ExecutorCompletionService<>(recoveryExecutor);
LogReplayer replayer = new LogReplayer(recoverLogs, false);
versionInfo.blockUpdates();
try {
state = State.REPLAYING;
} finally {
versionInfo.unblockUpdates();
}
// At this point, we are guaranteed that any new updates coming in will see the state as "replaying"
return cs.submit(replayer, recoveryInfo);
}
示例15: applyBufferedUpdates
import java.util.concurrent.ExecutorCompletionService; //导入方法依赖的package包/类
/** Returns the Future to wait on, or null if no replay was needed */
public Future<RecoveryInfo> applyBufferedUpdates() {
// recovery trips this assert under some race - even when
// it checks the state first
// assert state == State.BUFFERING;
// block all updates to eliminate race conditions
// reading state and acting on it in the update processor
versionInfo.blockUpdates();
try {
cancelApplyBufferUpdate = false;
if (state != State.BUFFERING) return null;
operationFlags &= ~FLAG_GAP;
// handle case when no log was even created because no updates
// were received.
if (tlog == null) {
state = State.ACTIVE;
return null;
}
tlog.incref();
state = State.APPLYING_BUFFERED;
} finally {
versionInfo.unblockUpdates();
}
if (recoveryExecutor.isShutdown()) {
tlog.decref();
throw new RuntimeException("executor is not running...");
}
ExecutorCompletionService<RecoveryInfo> cs = new ExecutorCompletionService<>(recoveryExecutor);
LogReplayer replayer = new LogReplayer(Arrays.asList(new TransactionLog[]{tlog}), true);
return cs.submit(replayer, recoveryInfo);
}