本文整理汇总了Java中java.util.concurrent.ExecutorService.shutdownNow方法的典型用法代码示例。如果您正苦于以下问题:Java ExecutorService.shutdownNow方法的具体用法?Java ExecutorService.shutdownNow怎么用?Java ExecutorService.shutdownNow使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ExecutorService
的用法示例。
在下文中一共展示了ExecutorService.shutdownNow方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCreateSessionDoesNotBlockWhenNotConfiguredTo
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* Tests the behavior of the sessionPool of the PooledConnectionFactory when
* maximum number of sessions are reached.
*
* @throws Exception
*/
@Test(timeout = 60000)
public void testCreateSessionDoesNotBlockWhenNotConfiguredTo() throws Exception {
// using separate thread for testing so that we can interrupt the test
// if the call to get a new session blocks.
// start test runner thread
ExecutorService executor = Executors.newSingleThreadExecutor();
final Future<Boolean> result = executor.submit(new TestRunner());
boolean testPassed = Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisfied() throws Exception {
return result.isDone() && result.get().booleanValue();
}
}, TimeUnit.SECONDS.toMillis(10), TimeUnit.MILLISECONDS.toMillis(50));
if (!testPassed) {
PooledConnectionFactoryTest.LOG.error("2nd call to createSession() " +
"is blocking but should have returned an error instead.");
executor.shutdownNow();
fail("SessionPool inside PooledConnectionFactory is blocking if " +
"limit is exceeded but should return an exception instead.");
}
}
示例2: executorShutdown
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
private static void executorShutdown() {
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
System.out.println("shutdown executor");
executor.shutdown();
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
System.err.println("tasks interrupted");
} finally {
if (!executor.isTerminated()) {
System.err.println("cancel non-finished tasks");
}
executor.shutdownNow();
System.out.println("shutdown finished");
}
}
示例3: joinPool
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* Waits out termination of a thread pool or fails doing so.
*/
void joinPool(ExecutorService pool) {
try {
pool.shutdown();
if (!pool.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)) {
try {
threadFail("ExecutorService " + pool +
" did not terminate in a timely manner");
} finally {
// last resort, for the benefit of subsequent tests
pool.shutdownNow();
pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS);
}
}
} catch (SecurityException ok) {
// Allowed in case test doesn't have privs
} catch (InterruptedException fail) {
threadFail("Unexpected InterruptedException");
}
}
示例4: test
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
@Test
public void test() throws InterruptedException {
AtomicInteger incrementor = new AtomicInteger();
ResettableEvent are = new ResettableEvent(true);
ResettableEvent done = new ResettableEvent(false);
ExecutorService threadPool = Executors.newFixedThreadPool(3);
for (int i = 0; i < 10; i++) {
threadPool.submit(Interrupted.unchecked(() -> {
are.getAndReset();
incrementor.incrementAndGet();
done.set();
}));
}
done.getAndReset();
Thread.sleep(100); // give a little time to other threads to increment, if there's indeed a bug
assertEquals(1, incrementor.get());
threadPool.shutdownNow();
}
示例5: main
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException {
TestSync testSync = new TestSync();
ExecutorService es = Executors.newFixedThreadPool(200);
int times = 1000000;
for (int i = 0; i < times; i++) {
es.submit(testSync::add);
es.submit(TestSync::addStatic);
}
TimeUnit.SECONDS.sleep(1);
es.shutdownNow();
System.out.println("should be: " + times * 2 + ", actual is: " + testSync.getI());
}
示例6: verifyConcurrentServiceTicketGeneration
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
@Test
@IfProfileValue(name="cas.jpa.concurrent", value="true")
public void verifyConcurrentServiceTicketGeneration() throws Exception {
final TicketGrantingTicket newTgt = newTGT();
addTicketInTransaction(newTgt);
final ExecutorService executor = Executors.newFixedThreadPool(CONCURRENT_SIZE);
try {
final List<ServiceTicketGenerator> generators = new ArrayList<>(CONCURRENT_SIZE);
for (int i = 0; i < CONCURRENT_SIZE; i++) {
generators.add(new ServiceTicketGenerator(newTgt.getId(), this.jpaTicketRegistry, this.txManager));
}
final List<Future<String>> results = executor.invokeAll(generators);
for (final Future<String> result : results) {
assertNotNull(result.get());
}
} catch (final Exception e) {
logger.debug("testConcurrentServiceTicketGeneration produced an error", e);
fail("testConcurrentServiceTicketGeneration failed.");
} finally {
executor.shutdownNow();
}
}
示例7: verifyConcurrentServiceTicketGeneration
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
@Test
public void verifyConcurrentServiceTicketGeneration() throws Exception {
final TicketGrantingTicket newTgt = newTGT();
addTicketInTransaction(newTgt);
final ExecutorService executor = Executors.newFixedThreadPool(CONCURRENT_SIZE);
try {
final List<ServiceTicketGenerator> generators = new ArrayList<>(CONCURRENT_SIZE);
for (int i = 0; i < CONCURRENT_SIZE; i++) {
generators.add(new ServiceTicketGenerator(newTgt.getId(), this.ticketRegistry, this.txManager));
}
final List<Future<String>> results = executor.invokeAll(generators);
for (final Future<String> result : results) {
assertNotNull(result.get());
}
} catch (final Exception e) {
LOGGER.error("testConcurrentServiceTicketGeneration produced an error", e);
fail("testConcurrentServiceTicketGeneration failed.");
} finally {
executor.shutdownNow();
}
assertEquals(CONCURRENT_SIZE, this.ticketRegistry.getTickets().size() - 1);
}
示例8: generateUserAccountKey
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
public synchronized void generateUserAccountKey(User user) {
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(() -> {
new RetryerHelper<User>(this).retry(5, TimeUnit.SECONDS, 30,
new AccountKeyCallableTask(user), IOException.class);
});
executorService.shutdown();
try {
executorService.awaitTermination(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {
log.error(e);
}
executorService.shutdownNow();
}
示例9: fillMemoryBlockMultiThreaded
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
private static void fillMemoryBlockMultiThreaded(final Instance instance) {
ExecutorService service = Executors.newFixedThreadPool(instance.getLanes());
List<Future<?>> futures = new ArrayList<Future<?>>();
for (int i = 0; i < instance.getIterations(); i++) {
for (int j = 0; j < ARGON2_SYNC_POINTS; j++) {
for (int k = 0; k < instance.getLanes(); k++) {
final Position position = new Position(i, k, j, 0);
Future future = service.submit(new Runnable() {
@Override
public void run() {
FillSegment.fillSegment(instance, position);
}
});
futures.add(future);
}
joinThreads(instance, futures);
}
}
service.shutdownNow();
}
示例10: main
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception
{
int ponder = 5;
if (args.length > 0)
{
ponder = Integer.parseInt(args[0]);
}
int size = 5;
if (args.length > 1)
{
size = Integer.parseInt(args[1]);
}
ExecutorService exec = Executors.newCachedThreadPool();
Chopstick[] sticks = new Chopstick[size];
for (int i = 0; i < size; i++)
{
sticks[i] = new Chopstick();
}
for (int i = 0; i < size; i++)
{
exec.execute(new Philosopher(sticks[i], sticks[(i + 1) % size], i,
ponder));
}
if (args.length == 3 && args[2].equals("timeout"))
{
TimeUnit.SECONDS.sleep(5);
} else
{
System.out.println("Press 'Enter' to quit");
System.in.read();
}
exec.shutdownNow();
}
示例11: stop
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
public static void stop(ExecutorService executor) {
try {
executor.shutdown();
executor.awaitTermination(60, TimeUnit.SECONDS);
}
catch (InterruptedException e) {
System.err.println("termination interrupted");
}
finally {
if (!executor.isTerminated()) {
System.err.println("killing non-finished tasks");
}
executor.shutdownNow();
}
}
示例12: cleanupExecutor
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* Perform proper ThreadPool cleanup.
*/
private final void cleanupExecutor(ExecutorService executor)
{
@SuppressWarnings("unused")
List<Runnable> pending = executor.shutdownNow();
// Can pending.size() be > 0?
}
示例13: closeVerifyTimeout
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
private void closeVerifyTimeout(final ConsumerCoordinator coordinator,
final long closeTimeoutMs, final long requestTimeoutMs,
long expectedMinTimeMs, long expectedMaxTimeMs) throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
boolean coordinatorUnknown = coordinator.coordinatorUnknown();
// Run close on a different thread. Coordinator is locked by this thread, so it is
// not safe to use the coordinator from the main thread until the task completes.
Future<?> future = executor.submit(new Runnable() {
@Override
public void run() {
coordinator.close(Math.min(closeTimeoutMs, requestTimeoutMs));
}
});
// Wait for close to start. If coordinator is known, wait for close to queue
// at least one request. Otherwise, sleep for a short time.
if (!coordinatorUnknown)
client.waitForRequests(1, 1000);
else
Thread.sleep(200);
if (expectedMinTimeMs > 0) {
time.sleep(expectedMinTimeMs - 1);
try {
future.get(500, TimeUnit.MILLISECONDS);
fail("Close completed ungracefully without waiting for timeout");
} catch (TimeoutException e) {
// Expected timeout
}
}
if (expectedMaxTimeMs >= 0)
time.sleep(expectedMaxTimeMs - expectedMinTimeMs + 2);
future.get(2000, TimeUnit.MILLISECONDS);
} finally {
executor.shutdownNow();
}
}
示例14: main
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
public static void main(String[] args)
{
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++)
{
exec.execute(new LiftOff2());
}
Thread.yield();
exec.shutdownNow();
}
示例15: shutdownAndAwaitTermination
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* from javase7 doc
*
* @param pool
*/
private void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!pool.awaitTermination(5, TimeUnit.SECONDS)) {
pool.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!pool.awaitTermination(5, TimeUnit.SECONDS))
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
pool.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}