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


Java ForkJoinPool.invoke方法代码示例

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


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

示例1: test

import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
static void test(ForkJoinPool g, int i) throws Exception {
    boardSize = i;
    int ps = g.getParallelism();
    long start = System.nanoTime();
    NQueensCS task = new NQueensCS(new int[0]);
    g.invoke(task);
    int solutions = task.solutions;
    long time = System.nanoTime() - start;
    double secs = (double) time / NPS;
    if (solutions != expectedSolutions[i])
        throw new Error();
    System.out.printf("NQueensCS %3d", i);
    System.out.printf(" Time: %7.3f", secs);
    long sc = g.getStealCount();
    long ns = sc - lastStealCount;
    lastStealCount = sc;
    System.out.printf(" Steals/t: %5d", ns/ps);
    System.out.println();
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:20,代码来源:NQueensCS.java

示例2: main

import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
/**
 * @param args
 */
public static void main(String[] args) throws Exception {
	
	/*
	 * Create an array of 10000 elements
	 */
	int array[]=new int[10000];
	
	/*
	 * ForkJoinPool to execute the task 
	 */
	ForkJoinPool pool=new ForkJoinPool();
	
	/*
	 * Task to increment the elements of the array
	 */
	Task task=new Task("Task",array,0,array.length);
	
	/*
	 * Send the task to the pool
	 */
	pool.invoke(task);
	
	/*
	 * Shutdown the pool
	 */
	pool.shutdown();
	
	/*
	 * Write a message in the console
	 */
	System.out.printf("Main: End of the program.\n");
	
}
 
开发者ID:PacktPublishing,项目名称:Java-9-Concurrency-Cookbook-Second-Edition,代码行数:37,代码来源:Main.java

示例3: blur

import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
public static BufferedImage blur(BufferedImage srcImage) {
    int w = srcImage.getWidth();
    int h = srcImage.getHeight();

    int[] src = srcImage.getRGB(0, 0, w, h, null, 0, w);
    int[] dst = new int[src.length];

    System.out.println("Array size is " + src.length);
    System.out.println("Threshold is " + sThreshold);

    int processors = Runtime.getRuntime().availableProcessors();
    System.out.println(Integer.toString(processors) + " processor"
            + (processors != 1 ? "s are " : " is ")
            + "available");

    ForkBlur fb = new ForkBlur(src, 0, src.length, dst);

    ForkJoinPool pool = new ForkJoinPool();

    long startTime = System.currentTimeMillis();
    pool.invoke(fb);
    long endTime = System.currentTimeMillis();

    System.out.println("Image blur took " + (endTime - startTime) +
            " milliseconds.");

    BufferedImage dstImage =
            new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    dstImage.setRGB(0, 0, w, h, dst, 0, w);

    return dstImage;
}
 
开发者ID:chaokunyang,项目名称:jkes,代码行数:33,代码来源:ForkBlur.java

示例4: testPollSubmission

import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
/**
 * pollSubmission returns unexecuted submitted task, if present
 */
public void testPollSubmission() {
    final CountDownLatch done = new CountDownLatch(1);
    final ForkJoinTask a = ForkJoinTask.adapt(awaiter(done));
    final ForkJoinTask b = ForkJoinTask.adapt(awaiter(done));
    final ForkJoinTask c = ForkJoinTask.adapt(awaiter(done));
    final ForkJoinPool p = singletonPool();
    try (PoolCleaner cleaner = cleaner(p, done)) {
        Thread external = new Thread(new CheckedRunnable() {
            public void realRun() {
                p.execute(a);
                p.execute(b);
                p.execute(c);
            }});
        RecursiveAction s = new CheckedRecursiveAction() {
            protected void realCompute() {
                external.start();
                try {
                    external.join();
                } catch (Exception ex) {
                    threadUnexpectedException(ex);
                }
                assertTrue(p.hasQueuedSubmissions());
                assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread);
                ForkJoinTask r = ForkJoinTask.pollSubmission();
                assertTrue(r == a || r == b || r == c);
                assertFalse(r.isDone());
            }};
        p.invoke(s);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:ForkJoinTask8Test.java

示例5: testIsQuiescent

import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
/**
 * After invoking a single task, isQuiescent eventually becomes
 * true, at which time queues are empty, threads are not active,
 * the task has completed successfully, and construction
 * parameters continue to hold
 */
public void testIsQuiescent() throws Exception {
    ForkJoinPool p = new ForkJoinPool(2);
    try (PoolCleaner cleaner = cleaner(p)) {
        assertTrue(p.isQuiescent());
        long startTime = System.nanoTime();
        FibTask f = new FibTask(20);
        p.invoke(f);
        assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
                   p.getFactory());
        while (! p.isQuiescent()) {
            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
                throw new AssertionFailedError("timed out");
            assertFalse(p.getAsyncMode());
            assertFalse(p.isShutdown());
            assertFalse(p.isTerminating());
            assertFalse(p.isTerminated());
            Thread.yield();
        }

        assertTrue(p.isQuiescent());
        assertFalse(p.getAsyncMode());
        assertEquals(0, p.getQueuedTaskCount());
        assertEquals(0, p.getQueuedSubmissionCount());
        assertFalse(p.hasQueuedSubmissions());
        while (p.getActiveThreadCount() != 0
               && millisElapsedSince(startTime) < LONG_DELAY_MS)
            Thread.yield();
        assertFalse(p.isShutdown());
        assertFalse(p.isTerminating());
        assertFalse(p.isTerminated());
        assertTrue(f.isDone());
        assertEquals(6765, (int) f.get());
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:42,代码来源:ForkJoinPoolTest.java

示例6: testInvokeOnPool

import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
private <T> T testInvokeOnPool(ForkJoinPool pool, RecursiveTask<T> a) {
    try (PoolCleaner cleaner = cleaner(pool)) {
        checkNotDone(a);

        T result = pool.invoke(a);

        checkCompletedNormally(a, result);
        return result;
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:RecursiveTaskTest.java

示例7: main

import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    // 创建随机数组成的数组:
    long[] array = new long[400];
    fillRandom(array);

    // fork/join task:
    ForkJoinPool fjp = new ForkJoinPool(4); // 最大并发数4
    ForkJoinTask<Long> task = new SumTask(array, 0, array.length);
    long startTime = System.currentTimeMillis();
    Long result = fjp.invoke(task);
    long endTime = System.currentTimeMillis();
    System.out.println("Fork/join sum: " + result + " in " + (endTime - startTime) + " ms.");
}
 
开发者ID:daishicheng,项目名称:outcomes,代码行数:14,代码来源:Main.java

示例8: qsort

import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
public void qsort(SortableCollection<E> sortable, int start, int end) {
    ForkJoinPool pool = new ForkJoinPool();
    pool.invoke(new RASort(sortable,start,end));
}
 
开发者ID:PacktPublishing,项目名称:Java-9-Programming-By-Example,代码行数:5,代码来源:FJQuickSort.java

示例9: computeArea

import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
static double computeArea(ForkJoinPool pool, double l, double r) {
    SQuad q = new SQuad(l, r, 0);
    pool.invoke(q);
    return q.area;
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:6,代码来源:Integrate.java

示例10: demo3_ForkJoin_invoke

import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
private static void demo3_ForkJoin_invoke() {
    System.out.println();

    AverageSpeed averageSpeed = createTask();

    ForkJoinPool commonPool = ForkJoinPool.commonPool();
    double result = commonPool.invoke(averageSpeed);

    System.out.println("result = " + result);
}
 
开发者ID:PacktPublishing,项目名称:Java-9-Cookbook,代码行数:11,代码来源:Chapter07Concurrency04.java

示例11: main

import java.util.concurrent.ForkJoinPool; //导入方法依赖的package包/类
public static void main(String[] args) {
    Instant now = Instant.now();


    ForkJoinPool pool = new ForkJoinPool();

    ForkJoinTask<Long> task = new ForkJoinSumCalculate(0,10000000l);
    Long sum = pool.invoke(task);
    System.out.println(sum);
    Instant end = Instant.now();

    System.out.println("耗费的时间是:" + Duration.between(now,end).toMillis());

}
 
开发者ID:ZhangJin1988,项目名称:juc,代码行数:15,代码来源:TestForkJoinPool.java


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