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


Java ThreadPoolExecutor.execute方法代码示例

本文整理汇总了Java中java.util.concurrent.ThreadPoolExecutor.execute方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadPoolExecutor.execute方法的具体用法?Java ThreadPoolExecutor.execute怎么用?Java ThreadPoolExecutor.execute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.ThreadPoolExecutor的用法示例。


在下文中一共展示了ThreadPoolExecutor.execute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testIsTerminated

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * isTerminated is false before termination, true after
 */
public void testIsTerminated() throws InterruptedException {
    final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        final CountDownLatch done = new CountDownLatch(1);
        assertFalse(p.isTerminated());
        p.execute(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                assertFalse(p.isTerminated());
                threadStarted.countDown();
                await(done);
            }});
        await(threadStarted);
        assertFalse(p.isTerminating());
        done.countDown();
        try { p.shutdown(); } catch (SecurityException ok) { return; }
        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
        assertTrue(p.isTerminated());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:ScheduledExecutorTest.java

示例2: testIsTerminating

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * isTerminating is not true when running or when terminated
 */
public void testIsTerminating() throws InterruptedException {
    final ThreadPoolExecutor p =
        new ThreadPoolExecutor(1, 1,
                               LONG_DELAY_MS, MILLISECONDS,
                               new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        final CountDownLatch done = new CountDownLatch(1);
        assertFalse(p.isTerminating());
        p.execute(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                assertFalse(p.isTerminating());
                threadStarted.countDown();
                await(done);
            }});
        await(threadStarted);
        assertFalse(p.isTerminating());
        done.countDown();
        try { p.shutdown(); } catch (SecurityException ok) { return; }
        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
        assertTrue(p.isTerminated());
        assertFalse(p.isTerminating());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:28,代码来源:ThreadPoolExecutorTest.java

示例3: testIsTerminated

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * isTerminated is false before termination, true after
 */
public void testIsTerminated() throws InterruptedException {
    final ThreadPoolExecutor p =
        new CustomTPE(1, 1,
                      LONG_DELAY_MS, MILLISECONDS,
                      new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        final CountDownLatch done = new CountDownLatch(1);
        assertFalse(p.isTerminating());
        p.execute(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                assertFalse(p.isTerminating());
                threadStarted.countDown();
                await(done);
            }});
        await(threadStarted);
        assertFalse(p.isTerminating());
        done.countDown();
        try { p.shutdown(); } catch (SecurityException ok) { return; }
        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
        assertTrue(p.isTerminated());
        assertFalse(p.isTerminating());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:28,代码来源:ThreadPoolExecutorSubclassTest.java

示例4: testRejectedRecycledTask

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * execute allows the same task to be submitted multiple times, even
 * if rejected
 */
public void testRejectedRecycledTask() throws InterruptedException {
    final int nTasks = 1000;
    final CountDownLatch done = new CountDownLatch(nTasks);
    final Runnable recycledTask = new Runnable() {
        public void run() {
            done.countDown();
        }};
    final ThreadPoolExecutor p =
        new ThreadPoolExecutor(1, 30,
                               60, SECONDS,
                               new ArrayBlockingQueue(30));
    try (PoolCleaner cleaner = cleaner(p)) {
        for (int i = 0; i < nTasks; ++i) {
            for (;;) {
                try {
                    p.execute(recycledTask);
                    break;
                }
                catch (RejectedExecutionException ignore) {}
            }
        }
        // enough time to run all tasks
        await(done, nTasks * SHORT_DELAY_MS);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:30,代码来源:ThreadPoolExecutorTest.java

示例5: testPurge

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * purge removes cancelled tasks from the queue
 */
public void testPurge() throws InterruptedException {
    final CountDownLatch threadStarted = new CountDownLatch(1);
    final CountDownLatch done = new CountDownLatch(1);
    final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
    final ThreadPoolExecutor p =
        new ThreadPoolExecutor(1, 1,
                               LONG_DELAY_MS, MILLISECONDS,
                               q);
    try (PoolCleaner cleaner = cleaner(p, done)) {
        FutureTask[] tasks = new FutureTask[5];
        for (int i = 0; i < tasks.length; i++) {
            Callable task = new CheckedCallable<Boolean>() {
                public Boolean realCall() throws InterruptedException {
                    threadStarted.countDown();
                    await(done);
                    return Boolean.TRUE;
                }};
            tasks[i] = new FutureTask(task);
            p.execute(tasks[i]);
        }
        await(threadStarted);
        assertEquals(tasks.length, p.getTaskCount());
        assertEquals(tasks.length - 1, q.size());
        assertEquals(1L, p.getActiveCount());
        assertEquals(0L, p.getCompletedTaskCount());
        tasks[4].cancel(true);
        tasks[3].cancel(false);
        p.purge();
        assertEquals(tasks.length - 3, q.size());
        assertEquals(tasks.length - 2, p.getTaskCount());
        p.purge();         // Nothing to do
        assertEquals(tasks.length - 3, q.size());
        assertEquals(tasks.length - 2, p.getTaskCount());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:39,代码来源:ThreadPoolExecutorTest.java

示例6: testDiscardOldestOnShutdown

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * execute using DiscardOldestPolicy drops task on shutdown
 */
public void testDiscardOldestOnShutdown() {
    final ThreadPoolExecutor p =
        new ThreadPoolExecutor(1, 1,
                               LONG_DELAY_MS, MILLISECONDS,
                               new ArrayBlockingQueue<Runnable>(1),
                               new DiscardOldestPolicy());

    try { p.shutdown(); } catch (SecurityException ok) { return; }
    try (PoolCleaner cleaner = cleaner(p)) {
        TrackedNoOpRunnable r = new TrackedNoOpRunnable();
        p.execute(r);
        assertFalse(r.done);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:ThreadPoolExecutorTest.java

示例7: testExecute

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * execute successfully executes a runnable
 */
public void testExecute() throws InterruptedException {
    final ThreadPoolExecutor p =
        new CustomTPE(1, 1,
                      2 * LONG_DELAY_MS, MILLISECONDS,
                      new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch done = new CountDownLatch(1);
        final Runnable task = new CheckedRunnable() {
            public void realRun() { done.countDown(); }};
        p.execute(task);
        await(done);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:ThreadPoolExecutorSubclassTest.java

示例8: test

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@Test
public void test() {
	
	int threads = 40;
	
	ThreadPoolExecutor executor = new ThreadPoolExecutor(threads, threads, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
	
	try {
		int total = 0;
		int jobs = 1000;
		int add = 1000;
		
		latch = new CountDownLatch(jobs);
		
		for (int i = 0; i < jobs; ++i) {
			executor.execute(new IncValueAsync(total, total+add));
			total += add;
		}
		
		try {
			latch.await();
		} catch (InterruptedException e) {
			throw new RuntimeException("Interrupted.", e);
		}
		
		log.info("Checking set ... set.size() == " + set.size());
		
		if (set.size() != 0) {
			testFailed("HashSet.add() / HashSet.remove() has concurrency issues!");
		}
		
		testOk();
		
	} finally {
		executor.shutdownNow();
	}
}
 
开发者ID:kefik,项目名称:Pogamut3,代码行数:38,代码来源:Test08_HashSet.java

示例9: testRemove

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * remove(task) removes queued task, and fails to remove active task
 */
public void testRemove() throws InterruptedException {
    final CountDownLatch done = new CountDownLatch(1);
    BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
    final ThreadPoolExecutor p =
        new ThreadPoolExecutor(1, 1,
                               LONG_DELAY_MS, MILLISECONDS,
                               q);
    try (PoolCleaner cleaner = cleaner(p, done)) {
        Runnable[] tasks = new Runnable[6];
        final CountDownLatch threadStarted = new CountDownLatch(1);
        for (int i = 0; i < tasks.length; i++) {
            tasks[i] = new CheckedRunnable() {
                public void realRun() throws InterruptedException {
                    threadStarted.countDown();
                    await(done);
                }};
            p.execute(tasks[i]);
        }
        await(threadStarted);
        assertFalse(p.remove(tasks[0]));
        assertTrue(q.contains(tasks[4]));
        assertTrue(q.contains(tasks[3]));
        assertTrue(p.remove(tasks[4]));
        assertFalse(p.remove(tasks[4]));
        assertFalse(q.contains(tasks[4]));
        assertTrue(q.contains(tasks[3]));
        assertTrue(p.remove(tasks[3]));
        assertFalse(q.contains(tasks[3]));
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:ThreadPoolExecutorTest.java

示例10: testExecute

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * execute successfully executes a runnable
 */
public void testExecute() throws InterruptedException {
    final ThreadPoolExecutor p =
        new ThreadPoolExecutor(1, 1,
                               LONG_DELAY_MS, MILLISECONDS,
                               new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch done = new CountDownLatch(1);
        final Runnable task = new CheckedRunnable() {
            public void realRun() { done.countDown(); }};
        p.execute(task);
        await(done);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:ThreadPoolExecutorTest.java

示例11: execute

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * Execute the task sometime in the future, using ThreadPools.
 */
public synchronized void execute(String root, Runnable task) {
  ThreadPoolExecutor executor = executors.get(root);
  if (executor == null) {
    throw new RuntimeException("Cannot find root " + root
        + " for execution of task " + task);
  } else {
    executor.execute(task);
  }
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:13,代码来源:AsyncDiskService.java

示例12: testGetCompletedTaskCount

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * getCompletedTaskCount increases, but doesn't overestimate,
 * when tasks complete
 */
public void testGetCompletedTaskCount() throws InterruptedException {
    final ThreadPoolExecutor p =
        new CustomTPE(2, 2,
                      LONG_DELAY_MS, MILLISECONDS,
                      new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        final CountDownLatch threadProceed = new CountDownLatch(1);
        final CountDownLatch threadDone = new CountDownLatch(1);
        assertEquals(0, p.getCompletedTaskCount());
        p.execute(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                threadStarted.countDown();
                assertEquals(0, p.getCompletedTaskCount());
                await(threadProceed);
                threadDone.countDown();
            }});
        await(threadStarted);
        assertEquals(0, p.getCompletedTaskCount());
        threadProceed.countDown();
        await(threadDone);
        long startTime = System.nanoTime();
        while (p.getCompletedTaskCount() != 1) {
            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
                fail("timed out");
            Thread.yield();
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:ThreadPoolExecutorSubclassTest.java

示例13: testGetTaskCount

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * getTaskCount increases, but doesn't overestimate, when tasks
 * submitted
 */
public void testGetTaskCount() throws InterruptedException {
    final int TASKS = 3;
    final CountDownLatch done = new CountDownLatch(1);
    final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
    try (PoolCleaner cleaner = cleaner(p, done)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        assertEquals(0, p.getTaskCount());
        assertEquals(0, p.getCompletedTaskCount());
        p.execute(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                threadStarted.countDown();
                await(done);
            }});
        await(threadStarted);
        assertEquals(1, p.getTaskCount());
        assertEquals(0, p.getCompletedTaskCount());
        for (int i = 0; i < TASKS; i++) {
            assertEquals(1 + i, p.getTaskCount());
            p.execute(new CheckedRunnable() {
                public void realRun() throws InterruptedException {
                    threadStarted.countDown();
                    assertEquals(1 + TASKS, p.getTaskCount());
                    await(done);
                }});
        }
        assertEquals(1 + TASKS, p.getTaskCount());
        assertEquals(0, p.getCompletedTaskCount());
    }
    assertEquals(1 + TASKS, p.getTaskCount());
    assertEquals(1 + TASKS, p.getCompletedTaskCount());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:36,代码来源:ScheduledExecutorTest.java

示例14: should_create_node_atom

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@Test
public void should_create_node_atom() throws Throwable {
    ThreadPoolExecutor threadPoolExecutor =
        new ThreadPoolExecutor(5, 5, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<>(5));

    final AtomicInteger size = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(5);
    final String root = "/flow-agent";

    zkClient.create(root, null);
    String agentNodePath = ZKPaths.makePath(root, "flow-atom");

    zkClient.delete(agentNodePath, false);

    for (int i = 0; i < 5; i++) {
        threadPoolExecutor.execute(() -> {
            try {
                zkClient.createEphemeral(agentNodePath);
                size.incrementAndGet();
            } catch (ZkException e) {
                System.out.println(e);
            } finally {
                latch.countDown();
            }
        });
    }

    latch.await(30, TimeUnit.SECONDS);
    Assert.assertEquals(1, size.get());
    zkClient.delete(root, true);
}
 
开发者ID:FlowCI,项目名称:flow-platform,代码行数:32,代码来源:ZkClientTest.java

示例15: testShutdownNow

import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
 * shutdownNow returns a list containing tasks that were not run,
 * and those tasks are drained from the queue
 */
public void testShutdownNow() throws InterruptedException {
    final int poolSize = 2;
    final int count = 5;
    final AtomicInteger ran = new AtomicInteger(0);
    final ThreadPoolExecutor p =
        new CustomTPE(poolSize, poolSize,
                      LONG_DELAY_MS, MILLISECONDS,
                      new ArrayBlockingQueue<Runnable>(10));
    final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
    Runnable waiter = new CheckedRunnable() { public void realRun() {
        threadsStarted.countDown();
        try {
            MILLISECONDS.sleep(2 * LONG_DELAY_MS);
        } catch (InterruptedException success) {}
        ran.getAndIncrement();
    }};
    for (int i = 0; i < count; i++)
        p.execute(waiter);
    await(threadsStarted);
    assertEquals(poolSize, p.getActiveCount());
    assertEquals(0, p.getCompletedTaskCount());
    final List<Runnable> queuedTasks;
    try {
        queuedTasks = p.shutdownNow();
    } catch (SecurityException ok) {
        return; // Allowed in case test doesn't have privs
    }
    assertTrue(p.isShutdown());
    assertTrue(p.getQueue().isEmpty());
    assertEquals(count - poolSize, queuedTasks.size());
    assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
    assertTrue(p.isTerminated());
    assertEquals(poolSize, ran.get());
    assertEquals(poolSize, p.getCompletedTaskCount());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:40,代码来源:ThreadPoolExecutorSubclassTest.java


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