本文整理了Java中java.util.concurrent.ExecutorService.execute()
方法的一些代码示例,展示了ExecutorService.execute()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ExecutorService.execute()
方法的具体详情如下:
包路径:java.util.concurrent.ExecutorService
类名称:ExecutorService
方法名:execute
暂无
canonical example by Tabnine
public void runThreadTask() {
ExecutorService service = Executors.newCachedThreadPool();
service.execute(
() -> {
// ... do something inside runnable task
});
service.shutdown();
}
代码示例来源:origin: square/okhttp
private void parallelDrainQueue(int threadCount) {
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
for (int i = 0; i < threadCount; i++) {
executor.execute(new NamedRunnable("Crawler %s", i) {
@Override protected void execute() {
try {
drainQueue();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
executor.shutdown();
}
代码示例来源:origin: iluwatar/java-design-patterns
/**
* Starts the logging clients.
*
* @throws IOException if any I/O error occurs.
*/
public void start() throws IOException {
LOGGER.info("Starting logging clients");
service.execute(new TcpLoggingClient("Client 1", 6666));
service.execute(new TcpLoggingClient("Client 2", 6667));
service.execute(new UdpLoggingClient("Client 3", 6668));
service.execute(new UdpLoggingClient("Client 4", 6668));
}
代码示例来源:origin: skylot/jadx
public synchronized Future<Boolean> process() {
if (future != null) {
return future;
}
ExecutorService shutdownExecutor = Executors.newSingleThreadExecutor();
FutureTask<Boolean> task = new ShutdownTask();
shutdownExecutor.execute(task);
shutdownExecutor.shutdown();
future = task;
return future;
}
代码示例来源:origin: apache/incubator-pinot
private List<ResultTable> processSegments(final String query, final BrokerRequest brokerRequest)
throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(10);
final List<ResultTable> resultTables = Collections.synchronizedList(new ArrayList<ResultTable>());
for (final SegmentQueryProcessor segmentQueryProcessor : _segmentQueryProcessorMap.values()) {
executorService.execute(new Runnable() {
@Override
public void run() {
try {
ResultTable resultTable = segmentQueryProcessor.process(brokerRequest);
if (resultTable != null) {
resultTables.add(resultTable);
}
} catch (Exception e) {
LOGGER.error("Exception caught while processing segment '{}'.", segmentQueryProcessor.getSegmentName(), e);
return;
}
}
});
}
executorService.shutdown();
executorService.awaitTermination(_timeoutInSeconds, TimeUnit.SECONDS);
return resultTables;
}
代码示例来源:origin: apache/kylin
private static Pair<List<String>, List<String>> resetCoprocessorOnHTables(final Admin hbaseAdmin, final Path hdfsCoprocessorJar, List<String> tableNames) throws IOException {
List<String> processedTables = Collections.synchronizedList(new ArrayList<String>());
List<String> failedTables = Collections.synchronizedList(new ArrayList<String>());
int nThread = Runtime.getRuntime().availableProcessors() * 2;
if (nThread > MAX_THREADS) {
nThread = MAX_THREADS;
}
logger.info("Use {} threads to do upgrade", nThread);
ExecutorService coprocessorPool = Executors.newFixedThreadPool(nThread);
CountDownLatch countDownLatch = new CountDownLatch(tableNames.size());
for (final String tableName : tableNames) {
coprocessorPool.execute(new ResetCoprocessorWorker(countDownLatch, hbaseAdmin, hdfsCoprocessorJar, tableName, processedTables, failedTables));
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
logger.error("reset coprocessor failed: ", e);
}
coprocessorPool.shutdown();
return new Pair<>(processedTables, failedTables);
}
代码示例来源:origin: evernote/android-job
private void waitUntilScheduled() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
JobConfig.getExecutorService().execute(new Runnable() {
@Override
public void run() {
latch.countDown();
}
});
latch.await(50, TimeUnit.SECONDS);
}
}
代码示例来源:origin: apache/hbase
readPool = Executors.newFixedThreadPool(readThreads,
new ThreadFactoryBuilder().setNameFormat(
"Reader=%d,bindAddress=" + bindAddress.getHostName() +
Reader reader = new Reader();
readers[i] = reader;
readPool.execute(reader);
LOG.info(getName() + ": started " + readThreads + " reader(s) listening on port=" + port);
代码示例来源:origin: LeonardoZ/java-concurrency-patterns
public static void usingSingleThreadExecutor() {
System.out.println("=== SingleThreadExecutor ===");
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
singleThreadExecutor.execute(() -> System.out.println("Print this."));
singleThreadExecutor.execute(() -> System.out.println("and this one to."));
singleThreadExecutor.shutdown();
try {
singleThreadExecutor.awaitTermination(4, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("\n\n");
}
代码示例来源:origin: voldemort/voldemort
this.numberOfFailures.set(0);
this.operationTimes = new long[numRequests];
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
final CountDownLatch latch = new CountDownLatch(numRequests);
final AtomicInteger index = new AtomicInteger(0);
executor.execute(new Runnable() {
latch.await();
} catch(InterruptedException e) {
e.printStackTrace();
executor.shutdownNow();
try {
executor.awaitTermination(3, TimeUnit.SECONDS);
} catch(InterruptedException e) {}
} finally {
代码示例来源:origin: google/guava
final CountDownLatch beforeFunction = new CountDownLatch(1);
executor.execute(
new Runnable() {
@Override
executor.shutdown();
assertTrue(executor.awaitTermination(5, SECONDS));
代码示例来源:origin: GlowstoneMC/Glowstone
/**
* Downloads the libraries.
*/
public void run() {
if (!directory.isDirectory() && !directory.mkdirs()) {
GlowServer.logger
.log(Level.SEVERE, "Could not create libraries directory: " + directory);
}
for (Library library : libraries) {
downloaderService.execute(new LibraryDownloader(library));
}
downloaderService.shutdown();
try {
if (!downloaderService.awaitTermination(1, TimeUnit.MINUTES)) {
downloaderService.shutdownNow();
}
} catch (InterruptedException e) {
GlowServer.logger.log(Level.SEVERE, "Library Manager thread interrupted: ", e);
}
}
代码示例来源:origin: androidquery/androidquery
public static void execute(Runnable job){
if(fetchExe == null){
fetchExe = Executors.newFixedThreadPool(NETWORK_POOL);
}
fetchExe.execute(job);
}
代码示例来源:origin: redisson/redisson
final CountDownLatch latch = new CountDownLatch(1);
executor.execute(new Runnable() {
@Override
public void run() {
while (!latch.await(1, TimeUnit.MILLISECONDS)) {
long now = System.nanoTime();
timeoutNano -= (now - start);
timeoutNano -= (System.nanoTime() - start);
executor.awaitTermination(timeoutNano, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
return false;
代码示例来源:origin: jdbi/jdbi
@Test
public void testConcurrentLoading() throws InterruptedException {
ExecutorService pool = Executors.newFixedThreadPool(10);
IntStream
.range(1, 10)
.forEach(id -> pool.execute(() -> testBaz(id)));
pool.awaitTermination(1000, TimeUnit.MILLISECONDS);
}
}
代码示例来源:origin: iBotPeaches/Apktool
public static String execAndReturn(String[] cmd) {
ExecutorService executor = Executors.newCachedThreadPool();
try {
ProcessBuilder builder = new ProcessBuilder(cmd);
builder.redirectErrorStream(true);
Process process = builder.start();
StreamCollector collector = new StreamCollector(process.getInputStream());
executor.execute(collector);
process.waitFor();
if (! executor.awaitTermination(15, TimeUnit.SECONDS)) {
executor.shutdownNow();
if (! executor.awaitTermination(5, TimeUnit.SECONDS)) {
System.err.println("Stream collector did not terminate.");
}
}
return collector.get();
} catch (IOException | InterruptedException e) {
return null;
}
}
代码示例来源:origin: apache/hive
@Test(timeout = 200000)
public void testMtt() {
final int baseAllocSizeLog2 = 3, maxAllocSizeLog2 = 10, totalSize = 8192,
baseAllocSize = 1 << baseAllocSizeLog2, maxAllocSize = 1 << maxAllocSizeLog2;
final int threadCount = maxAllocSizeLog2 - baseAllocSizeLog2 + 1;
final int iterCount = 500;
final BuddyAllocator a = create(maxAllocSize, 4, totalSize, true, false);
ExecutorService executor = Executors.newFixedThreadPool(threadCount + 1);
CountDownLatch cdlIn = new CountDownLatch(threadCount), cdlOut = new CountDownLatch(1);
@SuppressWarnings("unchecked")
FutureTask<MttTestCallableResult>[] allocTasks = new FutureTask[threadCount];
FutureTask<Void> dumpTask = createAllocatorDumpTask(a);
for (int allocSize = baseAllocSize, i = 0; allocSize <= maxAllocSize; allocSize <<= 1, ++i) {
allocTasks[i] = new FutureTask<>(new MttTestCallable(
cdlIn, cdlOut, a, allocSize, totalSize / allocSize, iterCount));
executor.execute(allocTasks[i]);
}
executor.execute(dumpTask);
runMttTest(a, allocTasks, cdlIn, cdlOut, dumpTask, null, null, totalSize, maxAllocSize);
}
代码示例来源:origin: JCTools/JCTools
@Setup(Level.Iteration)
public void startConsumers()
{
consumer.isRunning = true;
consumer.stopped = new CountDownLatch(consumerCount);
for (int i = 0; i < consumerCount; i++)
{
consumerExecutor.execute(consumer);
}
}
代码示例来源:origin: neo4j/neo4j
private static ExecutorService sameThreadExecutor() throws InterruptedException
{
ExecutorService executor = immediateExecutor();
when( executor.awaitTermination( anyLong(), any() ) ).thenReturn( true );
doAnswer( invocation ->
{
((Runnable) invocation.getArgument( 0 )).run();
return null;
} ).when( executor ).execute( any() );
return executor;
}
代码示例来源:origin: LeonardoZ/java-concurrency-patterns
public static void usingFixedThreadPool() {
System.out.println("=== FixedThreadPool ===");
ExecutorService fixedPool = Executors.newFixedThreadPool(4);
List<Future<UUID>> uuids = new LinkedList<>();
for (int i = 0; i < 20; i++) {
Future<UUID> submitted = fixedPool.submit(() -> {
UUID randomUUID = UUID.randomUUID();
System.out.println("UUID " + randomUUID + " from " + Thread.currentThread().getName());
return randomUUID;
});
uuids.add(submitted);
}
fixedPool.execute(() -> uuids.forEach((f) -> {
try {
System.out.println("Result " + f.get() + " from " + Thread.currentThread().getName());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}));
fixedPool.shutdown();
try {
fixedPool.awaitTermination(4, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("\n\n");
}
内容来源于网络,如有侵权,请联系作者删除!