本文整理汇总了Java中java.util.concurrent.ScheduledExecutorService.awaitTermination方法的典型用法代码示例。如果您正苦于以下问题:Java ScheduledExecutorService.awaitTermination方法的具体用法?Java ScheduledExecutorService.awaitTermination怎么用?Java ScheduledExecutorService.awaitTermination使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ScheduledExecutorService
的用法示例。
在下文中一共展示了ScheduledExecutorService.awaitTermination方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test1
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
@Test
public void test1 () throws Exception
{
final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor ();
final ProcessBuilder pb = new ProcessBuilder ( "date" ); // FIXME: works only on unix
final AbstractScheduledInput input = new ProcessInput ( executor, pb, Charset.forName ( "UTF-8" ), 1000 );
final TestListener listener = new TestListener ();
input.addInputListener ( listener );
input.start ();
Thread.sleep ( 100 );
input.stop ();
input.dispose ();
executor.shutdown ();
executor.awaitTermination ( Long.MAX_VALUE, TimeUnit.MINUTES );
dumpData ( listener );
// TODO: test
}
示例2: killTasks
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
public static void killTasks(final String name) {
final ScheduledExecutorService executorService = getBackgroundExecutor(name);
executorService.shutdownNow();
try {
executorService.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Log.wtf(TAG, "Failed to shut down: " + name);
}
if (executorService == sExecutorServiceForTests) {
// Don't do anything to the test service.
return;
}
switch (name) {
case KEYBOARD:
sKeyboardExecutorService = newExecutorService(KEYBOARD);
break;
case SPELLING:
sSpellingExecutorService = newExecutorService(SPELLING);
break;
default:
throw new IllegalArgumentException("Invalid executor: " + name);
}
}
示例3: testDelay
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
@Test
public void testDelay() throws InterruptedException {
ScheduledExecutorService ses =
Executors.newSingleThreadScheduledExecutor();
SingletonTask st1 = new SingletonTask(ses, new Runnable() {
@Override
public void run() {
ran += 1;
time = System.nanoTime();
}
});
st1.reschedule(10, TimeUnit.MILLISECONDS);
assertFalse("Check that task hasn't run yet", ran > 0);
ses.shutdown();
ses.awaitTermination(5, TimeUnit.SECONDS);
assertEquals("Check that task ran", 1, ran);
}
示例4: testBasic
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
@Test
public void testBasic() throws InterruptedException {
ScheduledExecutorService ses =
Executors.newSingleThreadScheduledExecutor();
SingletonTask st1 = new SingletonTask(ses, new Runnable() {
@Override
public void run() {
ran += 1;
}
});
st1.reschedule(0, null);
ses.shutdown();
ses.awaitTermination(5, TimeUnit.SECONDS);
assertEquals("Check that task ran", 1, ran);
}
示例5: testScheduledExecutorServiceCaptures
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
@Test
public void testScheduledExecutorServiceCaptures() throws InterruptedException {
// Setup
ScheduledExecutorService e = Executors.newScheduledThreadPool(10);
ScheduledExecutorService f = StateCapture.capturingDecorator(e);
CapturedState mockCapturedState = mock(CapturedState.class);
Runnable mockRunnable = mock(Runnable.class);
ThreadLocalStateCaptor.THREAD_LOCAL.set(mockCapturedState);
f.execute(mockRunnable);
e.shutdown();
e.awaitTermination(10, TimeUnit.HOURS);
verifyStandardCaptures(mockCapturedState, mockRunnable);
}
示例6: shutdown
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
public static void shutdown(ScheduledExecutorService executor, long timeoutMillis) {
if (executor == null) {
return;
}
executor.shutdownNow();
boolean cleanlyTerminated;
try {
cleanlyTerminated = executor.awaitTermination(timeoutMillis, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
cleanlyTerminated = executor.isTerminated();
}
if (!cleanlyTerminated) {
String threadpoolName;
if (executor instanceof ScheduledThreadPoolExecutor) {
ThreadFactory factory = ((ScheduledThreadPoolExecutor) executor).getThreadFactory();
if (factory instanceof NamingThreadFactory) {
NamingThreadFactory namingFactory = (NamingThreadFactory) factory;
threadpoolName = namingFactory.getPrefix();
} else {
threadpoolName = "unknown[" + factory.getClass().getSimpleName() + "]";
}
} else {
threadpoolName = "unknown[" + executor.getClass().getSimpleName() + "]";
}
LOG.error("executor did not terminate in the specified time: " + threadpoolName);
}
}
示例7: testReschedule
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
/**
* Test to make sure we reschedule the task for execution if it has already in progress.
*/
@Test
public void testReschedule() throws Exception {
ScheduledExecutorService ex = Executors.newScheduledThreadPool(1);
OneTaskOnlyExecutor decorator = new OneTaskOnlyExecutor(ex);
final CountDownLatch taskRunning = new CountDownLatch(1);
final CountDownLatch continueTask = new CountDownLatch(1);
final AtomicInteger counter = new AtomicInteger();
Callable waitForLatch = new Callable() {
public Object call() throws Exception {
taskRunning.countDown();
continueTask.await();
counter.incrementAndGet();
return null;
}
};
Runnable increment = new Runnable() {
public void run() {
counter.incrementAndGet();
}
};
decorator.schedule(waitForLatch, 0, TimeUnit.SECONDS);
taskRunning.await();
decorator.schedule(increment, 0, TimeUnit.SECONDS);
assertEquals(0, counter.get());
continueTask.countDown();
ex.shutdown();
ex.awaitTermination(60, TimeUnit.SECONDS);
assertEquals(2, counter.get());
}
示例8: testReschedule
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
@Test
public void testReschedule() throws InterruptedException {
ScheduledExecutorService ses =
Executors.newSingleThreadScheduledExecutor();
final Object tc = this;
SingletonTask st1 = new SingletonTask(ses, new Runnable() {
@Override
public void run() {
synchronized (tc) {
ran += 1;
}
time = System.nanoTime();
}
});
st1.reschedule(20, TimeUnit.MILLISECONDS);
Thread.sleep(5);
assertFalse("Check that task hasn't run yet", ran > 0);
st1.reschedule(20, TimeUnit.MILLISECONDS);
Thread.sleep(5);
assertFalse("Check that task hasn't run yet", ran > 0);
st1.reschedule(20, TimeUnit.MILLISECONDS);
Thread.sleep(5);
assertFalse("Check that task hasn't run yet", ran > 0);
st1.reschedule(20, TimeUnit.MILLISECONDS);
Thread.sleep(5);
assertFalse("Check that task hasn't run yet", ran > 0);
st1.reschedule(20, TimeUnit.MILLISECONDS);
Thread.sleep(5);
assertFalse("Check that task hasn't run yet", ran > 0);
st1.reschedule(20, TimeUnit.MILLISECONDS);
Thread.sleep(5);
assertFalse("Check that task hasn't run yet", ran > 0);
st1.reschedule(20, TimeUnit.MILLISECONDS);
Thread.sleep(5);
assertFalse("Check that task hasn't run yet", ran > 0);
st1.reschedule(20, TimeUnit.MILLISECONDS);
Thread.sleep(5);
assertFalse("Check that task hasn't run yet", ran > 0);
ses.shutdown();
ses.awaitTermination(5, TimeUnit.SECONDS);
assertEquals("Check that task ran only once", 1, ran);
}
示例9: main
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
/**
* Main method of the example
* @param args
*/
public static void main(String[] args) {
// Create a ScheduledThreadPoolExecutor
ScheduledExecutorService executor=Executors.newScheduledThreadPool(1);
System.out.printf("Main: Starting at: %s\n",new Date());
// Send the tasks to the executor with the specified delay
for (int i=0; i<5; i++) {
Task task=new Task("Task "+i);
executor.schedule(task,i+1 , TimeUnit.SECONDS);
}
// Finish the executor
executor.shutdown();
// Waits for the finalization of the executor
try {
executor.awaitTermination(1, TimeUnit.DAYS);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Writes the finalization message
System.out.printf("Core: Ends at: %s\n",new Date());
}
示例10: testConcurrentAddDelay2
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
@Test
public void testConcurrentAddDelay2() throws InterruptedException {
ScheduledExecutorService ses =
Executors.newSingleThreadScheduledExecutor();
final Object tc = this;
SingletonTask st1 = new SingletonTask(ses, new Runnable() {
@Override
public void run() {
synchronized (tc) {
ran += 1;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (tc) {
finished += 1;
time = System.nanoTime();
}
}
});
st1.reschedule(5, TimeUnit.MILLISECONDS);
Thread.sleep(20);
assertEquals("Check that task started", 1, ran);
assertEquals("Check that task not finished", 0, finished);
st1.reschedule(25, TimeUnit.MILLISECONDS);
assertTrue("Check task running state true", st1.context.taskRunning);
assertTrue("Check task should run state true", st1.context.taskShouldRun);
assertEquals("Check that task started", 1, ran);
assertEquals("Check that task not finished", 0, finished);
Thread.sleep(150);
assertTrue("Check task running state false", !st1.context.taskRunning);
assertTrue("Check task should run state false", !st1.context.taskShouldRun);
assertEquals("Check that task ran exactly twice", 2, ran);
assertEquals("Check that task finished exactly twice", 2, finished);
ses.shutdown();
ses.awaitTermination(5, TimeUnit.SECONDS);
}
示例11: testConcurrentAddNoDelay
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
@Test
public void testConcurrentAddNoDelay() throws InterruptedException {
ScheduledExecutorService ses =
Executors.newSingleThreadScheduledExecutor();
final Object tc = this;
SingletonTask st1 = new SingletonTask(ses, new Runnable() {
@Override
public void run() {
synchronized (tc) {
ran += 1;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (tc) {
finished += 1;
time = System.nanoTime();
}
}
});
st1.reschedule(0, null);
Thread.sleep(20);
assertEquals("Check that task started", 1, ran);
assertEquals("Check that task not finished", 0, finished);
st1.reschedule(0, null);
assertTrue("Check task running state true", st1.context.taskRunning);
assertTrue("Check task should run state true", st1.context.taskShouldRun);
assertEquals("Check that task started", 1, ran);
assertEquals("Check that task not finished", 0, finished);
Thread.sleep(150);
assertTrue("Check task running state false", !st1.context.taskRunning);
assertTrue("Check task should run state false", !st1.context.taskShouldRun);
assertEquals("Check that task ran exactly twice", 2, ran);
assertEquals("Check that task finished exactly twice", 2, finished);
ses.shutdown();
ses.awaitTermination(5, TimeUnit.SECONDS);
}
示例12: testConcurrentAddDelay
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
@Test
public void testConcurrentAddDelay() throws InterruptedException {
ScheduledExecutorService ses =
Executors.newSingleThreadScheduledExecutor();
final Object tc = this;
SingletonTask st1 = new SingletonTask(ses, new Runnable() {
@Override
public void run() {
synchronized (tc) {
ran += 1;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (tc) {
finished += 1;
time = System.nanoTime();
}
}
});
st1.reschedule(5, TimeUnit.MILLISECONDS);
Thread.sleep(20);
assertEquals("Check that task started", 1, ran);
assertEquals("Check that task not finished", 0, finished);
st1.reschedule(75, TimeUnit.MILLISECONDS);
assertTrue("Check task running state true", st1.context.taskRunning);
assertTrue("Check task should run state true", st1.context.taskShouldRun);
assertEquals("Check that task started", 1, ran);
assertEquals("Check that task not finished", 0, finished);
Thread.sleep(150);
assertTrue("Check task running state false", !st1.context.taskRunning);
assertTrue("Check task should run state false", !st1.context.taskShouldRun);
assertEquals("Check that task ran exactly twice", 2, ran);
assertEquals("Check that task finished exactly twice", 2, finished);
ses.shutdown();
ses.awaitTermination(15, TimeUnit.SECONDS);
}
示例13: testWakeupWithFetchDataAvailable
import java.util.concurrent.ScheduledExecutorService; //导入方法依赖的package包/类
@Test
public void testWakeupWithFetchDataAvailable() throws Exception {
int rebalanceTimeoutMs = 60000;
final int sessionTimeoutMs = 30000;
int heartbeatIntervalMs = 3000;
// adjust auto commit interval lower than heartbeat so we don't need to deal with
// a concurrent heartbeat request
int autoCommitIntervalMs = 1000;
final Time time = new MockTime();
Cluster cluster = TestUtils.singletonCluster(topic, 1);
Node node = cluster.nodes().get(0);
Metadata metadata = createMetadata();
metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
MockClient client = new MockClient(time, metadata);
client.setNode(node);
PartitionAssignor assignor = new RoundRobinAssignor();
final KafkaConsumer<String, String> consumer = newConsumer(time, client, metadata, assignor,
rebalanceTimeoutMs, sessionTimeoutMs, heartbeatIntervalMs, true, autoCommitIntervalMs);
consumer.subscribe(Arrays.asList(topic), getConsumerRebalanceListener(consumer));
prepareRebalance(client, node, assignor, Arrays.asList(tp0), null);
consumer.poll(0);
// respond to the outstanding fetch so that we have data available on the next poll
client.respondFrom(fetchResponse(tp0, 0, 5), node);
client.poll(0, time.milliseconds());
consumer.wakeup();
try {
consumer.poll(0);
fail();
} catch (WakeupException e) {
}
// make sure the position hasn't been updated
assertEquals(0, consumer.position(tp0));
// the next poll should return the completed fetch
ConsumerRecords<String, String> records = consumer.poll(0);
assertEquals(5, records.count());
// Increment time asynchronously to clear timeouts in closing the consumer
final ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
time.sleep(sessionTimeoutMs);
}
}, 0L, 10L, TimeUnit.MILLISECONDS);
consumer.close();
exec.shutdownNow();
exec.awaitTermination(5L, TimeUnit.SECONDS);
}