类java.util.concurrent.ForkJoinWorkerThread源码实例Demo

下面列出了怎么用java.util.concurrent.ForkJoinWorkerThread的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: grpc-nebula-java   文件: LoadServer.java
ExecutorService getExecutor(int asyncThreads) {
  // TODO(carl-mastrangelo): This should not be necessary.  I don't know where this should be
  // put.  Move it somewhere else, or remove it if no longer necessary.
  // See: https://github.com/grpc/grpc-java/issues/2119
  return new ForkJoinPool(asyncThreads,
      new ForkJoinWorkerThreadFactory() {
        final AtomicInteger num = new AtomicInteger();
        @Override
        public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
          ForkJoinWorkerThread thread = defaultForkJoinWorkerThreadFactory.newThread(pool);
          thread.setDaemon(true);
          thread.setName("server-worker-" + "-" + num.getAndIncrement());
          return thread;
        }
      }, UncaughtExceptionHandlers.systemExit(), true /* async */);
}
 
源代码2 项目: grpc-java   文件: LoadServer.java
ExecutorService getExecutor(int asyncThreads) {
  // TODO(carl-mastrangelo): This should not be necessary.  I don't know where this should be
  // put.  Move it somewhere else, or remove it if no longer necessary.
  // See: https://github.com/grpc/grpc-java/issues/2119
  return new ForkJoinPool(asyncThreads,
      new ForkJoinWorkerThreadFactory() {
        final AtomicInteger num = new AtomicInteger();
        @Override
        public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
          ForkJoinWorkerThread thread = defaultForkJoinWorkerThreadFactory.newThread(pool);
          thread.setDaemon(true);
          thread.setName("server-worker-" + "-" + num.getAndIncrement());
          return thread;
        }
      }, UncaughtExceptionHandlers.systemExit(), true /* async */);
}
 
源代码3 项目: buck   文件: MostExecutors.java
/**
 * Construct a ForkJoinPool with a stricter thread limit.
 *
 * <p>ForkJoinPool by default will create a new thread to handle pending work whenever an existing
 * thread becomes blocked on a task and cannot work steal. In cases when many tasks would block on
 * a slow running dependency, it can trigger thread creation for all those tasks.
 *
 * <p>Note that limiting the maximum threads will impact the ability for ManagedBlockers to cause
 * the pool to create new worker threads, leading to potential deadlock if many ManagedBlockers
 * are used.
 */
public static ForkJoinPool forkJoinPoolWithThreadLimit(int parallelism, int spares) {
  AtomicInteger activeThreads = new AtomicInteger(0);
  return new ForkJoinPool(
      parallelism,
      pool -> {
        if (activeThreads.get() > parallelism + spares) {
          return null;
        }
        return new ForkJoinWorkerThread(pool) {
          @Override
          protected void onStart() {
            super.onStart();
            activeThreads.incrementAndGet();
          }

          @Override
          protected void onTermination(Throwable exception) {
            activeThreads.decrementAndGet();
            super.onTermination(exception);
          }
        };
      },
      /* handler */ null,
      /* asyncMode */ false);
}
 
源代码4 项目: airsonic-advanced   文件: MediaScannerService.java
@Override
public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
    final ForkJoinWorkerThread worker = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool);
    worker.setName("MediaLibraryScanner-" + worker.getPoolIndex());
    worker.setPriority(Thread.MIN_PRIORITY);
    return worker;
}
 
源代码5 项目: dragonwell8_jdk   文件: AbstractTask.java
/**
 * Default target of leaf tasks for parallel decomposition.
 * To allow load balancing, we over-partition, currently to approximately
 * four tasks per processor, which enables others to help out
 * if leaf tasks are uneven or some processors are otherwise busy.
 */
public static int getLeafTarget() {
    Thread t = Thread.currentThread();
    if (t instanceof ForkJoinWorkerThread) {
        return ((ForkJoinWorkerThread) t).getPool().getParallelism() << 2;
    }
    else {
        return LEAF_TARGET;
    }
}
 
源代码6 项目: kylin-on-parquet-v2   文件: InMemCubeBuilder2.java
/**
 * Build all the cuboids and wait for all the tasks finished. 
 * 
 * @param input
 * @param listener
 * @return
 * @throws IOException
 */
private <T> NavigableMap<Long, CuboidResult> buildAndCollect(final RecordConsumeBlockingQueueController<T> input,
        final ICuboidResultListener listener) throws IOException {

    long startTime = System.currentTimeMillis();
    logger.info("In Mem Cube Build2 start, {}", cubeDesc.getName());

    // build base cuboid
    buildBaseCuboid(input, listener);

    ForkJoinWorkerThreadFactory factory = new ForkJoinWorkerThreadFactory() {
        @Override
        public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
            final ForkJoinWorkerThread worker = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool);
            worker.setName("inmem-cubing-cuboid-worker-" + worker.getPoolIndex());
            return worker;
        }
    };
    ForkJoinPool builderPool = new ForkJoinPool(taskThreadCount, factory, null, true);
    ForkJoinTask rootTask = builderPool.submit(new Runnable() {
        @Override
        public void run() {
            startBuildFromBaseCuboid();
        }
    });
    rootTask.join();

    long endTime = System.currentTimeMillis();
    logger.info("In Mem Cube Build2 end, {}, takes {} ms", cubeDesc.getName(), (endTime - startTime));
    logger.info("total CuboidResult count: {}", resultCollector.getAllResult().size());
    return resultCollector.getAllResult();
}
 
源代码7 项目: TencentKona-8   文件: AbstractTask.java
/**
 * Default target of leaf tasks for parallel decomposition.
 * To allow load balancing, we over-partition, currently to approximately
 * four tasks per processor, which enables others to help out
 * if leaf tasks are uneven or some processors are otherwise busy.
 */
public static int getLeafTarget() {
    Thread t = Thread.currentThread();
    if (t instanceof ForkJoinWorkerThread) {
        return ((ForkJoinWorkerThread) t).getPool().getParallelism() << 2;
    }
    else {
        return LEAF_TARGET;
    }
}
 
源代码8 项目: andesite-node   文件: CommonAsync.java
private static ForkJoinPool createPool() {
    AtomicInteger threadNumber = new AtomicInteger();
    return new ForkJoinPool(
            Runtime.getRuntime().availableProcessors(),
            pool -> {
                var thread = new ForkJoinWorkerThread(pool) {};
                thread.setDaemon(true);
                thread.setPriority((Thread.NORM_PRIORITY + Thread.MIN_PRIORITY) / 2);
                thread.setName("async-packet-provider-thread-" + (threadNumber.incrementAndGet()));
                return thread;
            },
            null,
            true
    );
}
 
源代码9 项目: openjdk-jdk8u   文件: AbstractTask.java
/**
 * Default target of leaf tasks for parallel decomposition.
 * To allow load balancing, we over-partition, currently to approximately
 * four tasks per processor, which enables others to help out
 * if leaf tasks are uneven or some processors are otherwise busy.
 */
public static int getLeafTarget() {
    Thread t = Thread.currentThread();
    if (t instanceof ForkJoinWorkerThread) {
        return ((ForkJoinWorkerThread) t).getPool().getParallelism() << 2;
    }
    else {
        return LEAF_TARGET;
    }
}
 
源代码10 项目: botbuilder-java   文件: ExecutorFactory.java
@Override
public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
    ForkJoinWorkerThread worker =
        ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool);
    worker.setName("Bot-" + worker.getPoolIndex());
    return worker;
}
 
源代码11 项目: Bytecoder   文件: AbstractTask.java
/**
 * Default target of leaf tasks for parallel decomposition.
 * To allow load balancing, we over-partition, currently to approximately
 * four tasks per processor, which enables others to help out
 * if leaf tasks are uneven or some processors are otherwise busy.
 */
public static int getLeafTarget() {
    Thread t = Thread.currentThread();
    if (t instanceof ForkJoinWorkerThread) {
        return ((ForkJoinWorkerThread) t).getPool().getParallelism() << 2;
    }
    else {
        return LEAF_TARGET;
    }
}
 
源代码12 项目: component-runtime   文件: Generator.java
public Tasks() {
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    executorService = new ForkJoinPool(Math.max(4, Runtime.getRuntime().availableProcessors() * 8),
            p -> new ForkJoinWorkerThread(p) {

                {
                    setContextClassLoader(loader);
                }
            }, (r, executor) -> errors.add(new IllegalStateException("Task rejected: " + r)), false);
}
 
源代码13 项目: openjdk-jdk9   文件: ForkJoinTask8Test.java
/**
 * 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);
    }
}
 
源代码14 项目: openjdk-jdk9   文件: RecursiveActionTest.java
/**
 * getPool of current thread in pool returns its pool
 */
public void testWorkerGetPool() {
    final ForkJoinPool mainPool = mainPool();
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            ForkJoinWorkerThread w =
                (ForkJoinWorkerThread) Thread.currentThread();
            assertSame(mainPool, w.getPool());
        }};
    testInvokeOnPool(mainPool, a);
}
 
源代码15 项目: openjdk-jdk9   文件: RecursiveActionTest.java
/**
 * getPoolIndex of current thread in pool returns 0 <= value < poolSize
 */
public void testWorkerGetPoolIndex() {
    final ForkJoinPool mainPool = mainPool();
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            ForkJoinWorkerThread w =
                (ForkJoinWorkerThread) Thread.currentThread();
            assertTrue(w.getPoolIndex() >= 0);
            // pool size can shrink after assigning index, so cannot check
            // assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
        }};
    testInvokeOnPool(mainPool, a);
}
 
源代码16 项目: jdk8u_jdk   文件: AbstractTask.java
/**
 * Default target of leaf tasks for parallel decomposition.
 * To allow load balancing, we over-partition, currently to approximately
 * four tasks per processor, which enables others to help out
 * if leaf tasks are uneven or some processors are otherwise busy.
 */
public static int getLeafTarget() {
    Thread t = Thread.currentThread();
    if (t instanceof ForkJoinWorkerThread) {
        return ((ForkJoinWorkerThread) t).getPool().getParallelism() << 2;
    }
    else {
        return LEAF_TARGET;
    }
}
 
@Test
public void prefixIsAddedToThread() {
  nexusForkJoinWorkerThreadFactory = new NexusForkJoinWorkerThreadFactory("prefix-test");
  ForkJoinPool forkJoinPool = new ForkJoinPool();
  ForkJoinWorkerThread thread = nexusForkJoinWorkerThreadFactory.newThread(forkJoinPool);
  assertThat(thread.getName(), CoreMatchers.containsString("prefix-test"));
}
 
源代码18 项目: Jupiter   文件: ForkJoinPoolExecutorFactory.java
@Override
public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
    // Note: The ForkJoinPool will create these threads as daemon threads.
    ForkJoinWorkerThread thread = new InternalForkJoinWorkerThread(pool);
    thread.setName(namePrefix + '-' + idx.getAndIncrement());
    return thread;
}
 
源代码19 项目: streamex   文件: CustomPoolTest.java
private void checkThread(Object element) {
    Thread thread = Thread.currentThread();
    if (!(thread instanceof ForkJoinWorkerThread))
        throw new IllegalStateException("Not inside FJP (element: " + element + ")");
    if (((ForkJoinWorkerThread) thread).getPool() != pool)
        throw new IllegalStateException("FJP is incorrect (element: " + element + ")");
}
 
源代码20 项目: kylin   文件: InMemCubeBuilder2.java
/**
 * Build all the cuboids and wait for all the tasks finished. 
 * 
 * @param input
 * @param listener
 * @return
 * @throws IOException
 */
private <T> NavigableMap<Long, CuboidResult> buildAndCollect(final RecordConsumeBlockingQueueController<T> input,
        final ICuboidResultListener listener) throws IOException {

    long startTime = System.currentTimeMillis();
    logger.info("In Mem Cube Build2 start, {}", cubeDesc.getName());

    // build base cuboid
    buildBaseCuboid(input, listener);

    ForkJoinWorkerThreadFactory factory = new ForkJoinWorkerThreadFactory() {
        @Override
        public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
            final ForkJoinWorkerThread worker = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool);
            worker.setName("inmem-cubing-cuboid-worker-" + worker.getPoolIndex());
            return worker;
        }
    };
    ForkJoinPool builderPool = new ForkJoinPool(taskThreadCount, factory, null, true);
    ForkJoinTask rootTask = builderPool.submit(new Runnable() {
        @Override
        public void run() {
            startBuildFromBaseCuboid();
        }
    });
    rootTask.join();

    long endTime = System.currentTimeMillis();
    logger.info("In Mem Cube Build2 end, {}, takes {} ms", cubeDesc.getName(), (endTime - startTime));
    logger.info("total CuboidResult count: {}", resultCollector.getAllResult().size());
    return resultCollector.getAllResult();
}
 
源代码21 项目: j2objc   文件: ForkJoinTask8Test.java
/**
 * 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);
    }
}
 
源代码22 项目: j2objc   文件: RecursiveActionTest.java
/**
 * getPool of current thread in pool returns its pool
 */
public void testWorkerGetPool() {
    final ForkJoinPool mainPool = mainPool();
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            ForkJoinWorkerThread w =
                (ForkJoinWorkerThread) Thread.currentThread();
            assertSame(mainPool, w.getPool());
        }};
    testInvokeOnPool(mainPool, a);
}
 
源代码23 项目: j2objc   文件: RecursiveActionTest.java
/**
 * getPoolIndex of current thread in pool returns 0 <= value < poolSize
 */
public void testWorkerGetPoolIndex() {
    final ForkJoinPool mainPool = mainPool();
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            ForkJoinWorkerThread w =
                (ForkJoinWorkerThread) Thread.currentThread();
            assertTrue(w.getPoolIndex() >= 0);
            // pool size can shrink after assigning index, so cannot check
            // assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
        }};
    testInvokeOnPool(mainPool, a);
}
 
源代码24 项目: a-foundation   文件: ForkJoinForkingPool.java
@Override public <T> ABenchmarkFuture<T> submit (Callable<T> code) {
    if (Thread.currentThread () instanceof ForkJoinWorkerThread) {
        final ForkJoinTask<T> task = ForkJoinTask.adapt (code);
        task.fork ();
        return new WrappingAFuture<> (task);
    }

    return new WrappingAFuture<> (ec.submit (code));
}
 
源代码25 项目: consulo   文件: IdeaForkJoinWorkerThreadFactory.java
@Override
public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
  final int n = setNextBit();
  ForkJoinWorkerThread thread = new ForkJoinWorkerThread(pool) {
    @Override
    protected void onTermination(Throwable exception) {
      clearBit(n);
      super.onTermination(exception);
    }
  };
  thread.setName("JobScheduler FJ pool " + n + "/" + PARALLELISM);
  thread.setPriority(Thread.NORM_PRIORITY - 1);
  return thread;
}
 
@Override
public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
    return new SafeForkJoinWorkerThread(pool);
}
 
源代码27 项目: vividus   文件: ContextCopyingExecutor.java
@Override
public ForkJoinWorkerThread newThread(ForkJoinPool pool)
{
    return new ContextAwareThread(pool, runContextData);
}
 
源代码28 项目: swim   文件: Theater.java
@Override
public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
  return new TheaterWorker(pool, this.theater, WORKER_COUNT.getAndIncrement(this));
}
 
源代码29 项目: dragonwell8_jdk   文件: ThreadLessCommon.java
@Override
public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
    return null;
}
 
源代码30 项目: kylin-on-parquet-v2   文件: DoggedCubeBuilder2.java
public <T> void build(BlockingQueue<T> input, InputConverterUnit<T> inputConverterUnit, ICuboidWriter output)
        throws IOException {
    final RecordConsumeBlockingQueueController<T> inputController = RecordConsumeBlockingQueueController
            .getQueueController(inputConverterUnit, input);

    final List<InMemCubeBuilder2> builderList = new CopyOnWriteArrayList<>();

    ForkJoinWorkerThreadFactory factory = new ForkJoinWorkerThreadFactory() {
        @Override
        public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
            final ForkJoinWorkerThread worker = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool);
            worker.setName("dogged-cubing-cuboid-worker-" + worker.getPoolIndex());
            return worker;
        }
    };

    ForkJoinPool builderPool = new ForkJoinPool(taskThreadCount, factory, null, true);
    CuboidResultWatcher resultWatcher = new CuboidResultWatcher(builderList, output);

    Stopwatch sw = new Stopwatch();
    sw.start();
    logger.info("Dogged Cube Build2 start");
    try {
        BaseCuboidTask<T> task = new BaseCuboidTask<>(inputController, 1, resultWatcher);
        builderPool.execute(task);
        do {
            builderList.add(task.getInternalBuilder());
            //Exception will be thrown here if cube building failure
            task.join();
            task = task.nextTask();
        } while (task != null);

        logger.info("Has finished feeding data, and base cuboid built, start to build child cuboids");
        for (final InMemCubeBuilder2 builder : builderList) {
            builderPool.submit(new Runnable() {
                @Override
                public void run() {
                    builder.startBuildFromBaseCuboid();
                }
            });
        }
        resultWatcher.start();
        logger.info("Dogged Cube Build2 splits complete, took " + sw.elapsedMillis() + " ms");
    } catch (Throwable e) {
        logger.error("Dogged Cube Build2 error", e);
        if (e instanceof Error)
            throw (Error) e;
        else if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        else
            throw new IOException(e);
    } finally {
        output.close();
        closeGirdTables(builderList);
        sw.stop();
        builderPool.shutdownNow();
        logger.info("Dogged Cube Build2 end, totally took " + sw.elapsedMillis() + " ms");
        logger.info("Dogged Cube Build2 return");
    }
}
 
 类所在包
 类方法
 同包方法