当前位置: 首页>>代码示例>>Java>>正文


Java ScheduledExecutorService.awaitTermination方法代码示例

本文整理汇总了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
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:25,代码来源:ProcessTest.java

示例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);
    }
}
 
开发者ID:sergeychilingaryan,项目名称:AOSP-Kayboard-7.1.2,代码行数:24,代码来源:ExecutorUtils.java

示例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);
}
 
开发者ID:nsg-ethz,项目名称:iTAP-controller,代码行数:21,代码来源:SingletonTaskTest.java

示例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);
}
 
开发者ID:xuraylei,项目名称:fresco_floodlight,代码行数:18,代码来源:SingletonTaskTest.java

示例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);
}
 
开发者ID:awslabs,项目名称:swage,代码行数:16,代码来源:StateCaptureTest.java

示例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);
	}
}
 
开发者ID:dernasherbrezon,项目名称:r2cloud,代码行数:29,代码来源:Util.java

示例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());
}
 
开发者ID:ampool,项目名称:monarch,代码行数:41,代码来源:OneTaskOnlyDecoratorJUnitTest.java

示例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);
}
 
开发者ID:nsg-ethz,项目名称:iTAP-controller,代码行数:47,代码来源:SingletonTaskTest.java

示例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());
}
 
开发者ID:PacktPublishing,项目名称:Java-9-Concurrency-Cookbook-Second-Edition,代码行数:31,代码来源:Main.java

示例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);
}
 
开发者ID:nsg-ethz,项目名称:iTAP-controller,代码行数:46,代码来源:SingletonTaskTest.java

示例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);
}
 
开发者ID:nsg-ethz,项目名称:iTAP-controller,代码行数:46,代码来源:SingletonTaskTest.java

示例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);
}
 
开发者ID:nsg-ethz,项目名称:iTAP-controller,代码行数:45,代码来源:SingletonTaskTest.java

示例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);
}
 
开发者ID:YMCoding,项目名称:kafka-0.11.0.0-src-with-comment,代码行数:60,代码来源:KafkaConsumerTest.java


注:本文中的java.util.concurrent.ScheduledExecutorService.awaitTermination方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。