java-concurrent
  • 前言
  • Java多线程基础
    • 线程简介
      • 什么是线程
      • 为什么要使用多线程/线程使用的好处
      • 线程的优先级
      • 线程的状态
      • Daemon线程
    • 启动和终止线程
      • 构造线程
      • 启动线程
      • 中断线程
      • 过期的suspend()、resume()和stop()
      • 安全地终止线程
    • 多线程实现方式
    • 多线程环境下,局部变量和全局变量都会共享吗?
    • Java线程间的协助和通信
      • Thread.join的使用
      • volatile、ThreadLocal、synchronized3个关键字区别
      • volatile关键字
      • ThreadLocal关键字
      • synchronized关键字
      • Java线程等待和通知的相关方法
    • 实战应用
      • 连接池
      • 线程池
      • 如何计算合适的线程数
  • Java线程池与框架
    • Executor 框架
    • 自定义线程池——ThreadPoolExecutor
    • 线程池工具类(单例模式)
    • 关闭线程池
    • 合理地配置线程池
    • 线程池的监控
    • RejectedExecutionException产生的原因
    • SpringBoot配置线程池工具类
    • FutureTask详解
    • CompletionService讲解
    • Future、FutureTask、CompletionService、CompletableFuture区别
  • Java内存模型
    • Java 内存模型的基础
      • 并发编程模型的两个关键问题
      • Java内存模型的抽象结构
      • 从源代码到指令序列的重排序
      • 并发编程模型的分类
    • 重排序
      • 数据依赖性
      • as-if-serial语义
      • 程序顺序规则
      • 重排序对多线程的影响
    • 顺序一致性
      • 数据竞争与顺序一致性
      • 顺序一致性内存模型
      • 同步程序的顺序一致性效果
      • 未同步程序的执行特性
    • volatile内存语义
      • volatile的特性
      • volatile写-读建立的happens-before关系
      • volatile写-读的内存语义
      • volatile内存语义的实现
      • JSR-133为什么要增强volatile的内存语义
    • 锁内存定义
      • 锁的释放-获取建立的happens-before关系
      • 锁的释放和获取的内存语义
      • 锁内存语义的实现
      • concurrent包的实现
    • final域内存语义
      • final域的重排序规则
      • 写final域的重排序规则
      • 读final域的重排序规则
      • final域为引用类型
      • 为什么final引用不能从构造函数内“溢出”
      • final语义在处理器中的实现
      • JSR-133为什么要增强final的语义
    • happens-before
    • 双重检查锁定与延迟初始化
      • 双重检查锁定的由来
      • 问题的根源
      • 基于volatile的解决方案
      • 基于类初始化的解决方案
    • Java内存模型综述
      • 处理器的内存模型
      • 各种内存模型之间的关系
      • JMM的内存可见性保证
      • JSR-133对旧内存模型的修补
  • HashMap实现原理
    • 讲解(一)
    • 讲解(二)
    • HashMap原理(面试篇)
    • HashMap原理(面试篇二)
  • ConcurrentHashMap的实现原理与使用
    • 为什么要使用ConcurrentHashMap
    • ConcurrentHashMap的结构
    • ConcurrentHashMap的初始化
    • 定位Segment
    • ConcurrentHashMap的操作
    • ConcurrentHashMap讲解(一)
  • Java中的阻塞队列
    • 什么是阻塞队列
    • Java里的阻塞队列
    • 阻塞队列的实现原理
  • Fork/Join框架
    • 什么是Fork/Join框架
    • 工作窃取算法
    • Fork/Join框架的设计
    • 使用Fork/Join框架
    • Fork/Join框架的异常处理
    • Fork/Join框架的实现原理
    • ForkJoinPool的commonPool相关参数配置
  • java.util.concurrent包讲解
    • 线程安全AtomicInteger的讲解
    • CompletableFuture讲解
      • CompletableFuture接口详解
      • CompletableFuture与parallelStream()性能差异
      • CompletableFuture接口详解2
  • Java线程安全
    • 性能与可伸缩性
    • 解决死锁
    • 死锁定义
    • 如何让多线程下的类安全
    • 类的线程安全性定义
    • 实战:实现一个线程安全的单例模式
  • Java常用并发开发工具和类的源码分析
    • CountDownLatch
    • CyclicBarrier
    • Semaphore
    • Exchange
    • ConcurrentHashMap
    • ConcurrentSkipListMap
    • HashMap
      • HashMap源码实现及分析
      • HashMap的一些面试题
    • List
  • Java中的锁
    • 基础知识
    • 番外篇
    • synchronized 是可重入锁吗?为什么?
    • 自旋锁
  • Java多线程的常见问题
    • 常见问题一
Powered by GitBook
On this page
  • 概述
  • commonPool
  • makeCommonPool
  • 配置参数
  • WorkQueue

Was this helpful?

  1. Fork/Join框架

ForkJoinPool的commonPool相关参数配置

概述

ForkJoinPool 主要用于实现“分而治之”的算法,特别是分治之后递归调用的函数,例如 quick sort 等。

ForkJoinPool 最适合的是计算密集型的任务,如果存在 I/O,线程间同步,sleep() 等会造成线程长时间阻塞的情况时,最好配合使用 ManagedBlocker。

commonPool

static {
        // initialize field offsets for CAS etc
        try {
            U = sun.misc.Unsafe.getUnsafe();
            Class<?> k = ForkJoinPool.class;
            CTL = U.objectFieldOffset
                (k.getDeclaredField("ctl"));
            RUNSTATE = U.objectFieldOffset
                (k.getDeclaredField("runState"));
            STEALCOUNTER = U.objectFieldOffset
                (k.getDeclaredField("stealCounter"));
            Class<?> tk = Thread.class;
            PARKBLOCKER = U.objectFieldOffset
                (tk.getDeclaredField("parkBlocker"));
            Class<?> wk = WorkQueue.class;
            QTOP = U.objectFieldOffset
                (wk.getDeclaredField("top"));
            QLOCK = U.objectFieldOffset
                (wk.getDeclaredField("qlock"));
            QSCANSTATE = U.objectFieldOffset
                (wk.getDeclaredField("scanState"));
            QPARKER = U.objectFieldOffset
                (wk.getDeclaredField("parker"));
            QCURRENTSTEAL = U.objectFieldOffset
                (wk.getDeclaredField("currentSteal"));
            QCURRENTJOIN = U.objectFieldOffset
                (wk.getDeclaredField("currentJoin"));
            Class<?> ak = ForkJoinTask[].class;
            ABASE = U.arrayBaseOffset(ak);
            int scale = U.arrayIndexScale(ak);
            if ((scale & (scale - 1)) != 0)
                throw new Error("data type scale not a power of two");
            ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
        } catch (Exception e) {
            throw new Error(e);
        }

        commonMaxSpares = DEFAULT_COMMON_MAX_SPARES;
        defaultForkJoinWorkerThreadFactory =
            new DefaultForkJoinWorkerThreadFactory();
        modifyThreadPermission = new RuntimePermission("modifyThread");

        common = java.security.AccessController.doPrivileged
            (new java.security.PrivilegedAction<ForkJoinPool>() {
                public ForkJoinPool run() { return makeCommonPool(); }});
        int par = common.config & SMASK; // report 1 even if threads disabled
        commonParallelism = par > 0 ? par : 1;
    }

makeCommonPool

/**
     * Creates and returns the common pool, respecting user settings
     * specified via system properties.
     */
    private static ForkJoinPool makeCommonPool() {
        int parallelism = -1;
        ForkJoinWorkerThreadFactory factory = null;
        UncaughtExceptionHandler handler = null;
        try {  // ignore exceptions in accessing/parsing properties
            String pp = System.getProperty
                ("java.util.concurrent.ForkJoinPool.common.parallelism");
            String fp = System.getProperty
                ("java.util.concurrent.ForkJoinPool.common.threadFactory");
            String hp = System.getProperty
                ("java.util.concurrent.ForkJoinPool.common.exceptionHandler");
            if (pp != null)
                parallelism = Integer.parseInt(pp);
            if (fp != null)
                factory = ((ForkJoinWorkerThreadFactory)ClassLoader.
                           getSystemClassLoader().loadClass(fp).newInstance());
            if (hp != null)
                handler = ((UncaughtExceptionHandler)ClassLoader.
                           getSystemClassLoader().loadClass(hp).newInstance());
        } catch (Exception ignore) {
        }
        if (factory == null) {
            if (System.getSecurityManager() == null)
                factory = defaultForkJoinWorkerThreadFactory;
            else // use security-managed default
                factory = new InnocuousForkJoinWorkerThreadFactory();
        }
        if (parallelism < 0 && // default 1 less than #cores
            (parallelism = Runtime.getRuntime().availableProcessors() - 1) <= 0)
            parallelism = 1;
        if (parallelism > MAX_CAP)
            parallelism = MAX_CAP;
        return new ForkJoinPool(parallelism, factory, handler, LIFO_QUEUE,
                                "ForkJoinPool.commonPool-worker-");
    }

配置参数

通过代码指定,必须得在commonPool初始化之前(parallel的stream被调用之前,一般可在系统启动后设置)注入进去,否则无法生效。

通过启动参数指定无此限制,较为安全

  • parallelism(即配置线程池个数)

    可以通过java.util.concurrent.ForkJoinPool.common.parallelism进行配置,最大值不能超过MAX_CAP,即32767.

static final int MAX_CAP = 0x7fff; //32767

如果没有指定,则默认为Runtime.getRuntime().availableProcessors() - 1.

代码指定(必须得在commonPool初始化之前注入进去,否则无法生效)

System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "8");

或者参数指定

-Djava.util.concurrent.ForkJoinPool.common.parallelism=8
  • threadFactory

默认为defaultForkJoinWorkerThreadFactory,没有securityManager的话。

/**
     * Default ForkJoinWorkerThreadFactory implementation; creates a
     * new ForkJoinWorkerThread.
     */
    static final class DefaultForkJoinWorkerThreadFactory
        implements ForkJoinWorkerThreadFactory {
        public final ForkJoinWorkerThread newThread(ForkJoinPool pool) {
            return new ForkJoinWorkerThread(pool);
        }
    }

代码指定(必须得在commonPool初始化之前注入进去,否则无法生效)

System.setProperty("java.util.concurrent.ForkJoinPool.common.threadFactory",YourForkJoinWorkerThreadFactory.class.getName());

参数指定

-Djava.util.concurrent.ForkJoinPool.common.threadFactory=com.xxx.xxx.YourForkJoinWorkerThreadFactory
  • exceptionHandler

如果没有设置,默认为null

/**
     * Callback from ForkJoinWorkerThread constructor to establish and
     * record its WorkQueue.
     *
     * @param wt the worker thread
     * @return the worker's queue
     */
    final WorkQueue registerWorker(ForkJoinWorkerThread wt) {
        UncaughtExceptionHandler handler;
        wt.setDaemon(true);                           // configure thread
        if ((handler = ueh) != null)
            wt.setUncaughtExceptionHandler(handler);
        WorkQueue w = new WorkQueue(this, wt);
        int i = 0;                                    // assign a pool index
        int mode = config & MODE_MASK;
        int rs = lockRunState();
        try {
            WorkQueue[] ws; int n;                    // skip if no array
            if ((ws = workQueues) != null && (n = ws.length) > 0) {
                int s = indexSeed += SEED_INCREMENT;  // unlikely to collide
                int m = n - 1;
                i = ((s << 1) | 1) & m;               // odd-numbered indices
                if (ws[i] != null) {                  // collision
                    int probes = 0;                   // step by approx half n
                    int step = (n <= 4) ? 2 : ((n >>> 1) & EVENMASK) + 2;
                    while (ws[i = (i + step) & m] != null) {
                        if (++probes >= n) {
                            workQueues = ws = Arrays.copyOf(ws, n <<= 1);
                            m = n - 1;
                            probes = 0;
                        }
                    }
                }
                w.hint = s;                           // use as random seed
                w.config = i | mode;
                w.scanState = i;                      // publication fence
                ws[i] = w;
            }
        } finally {
            unlockRunState(rs, rs & ~RSLOCK);
        }
        wt.setName(workerNamePrefix.concat(Integer.toString(i >>> 1)));
        return w;
    }

代码指定(必须得在commonPool初始化之前注入进去,否则无法生效)

System.setProperty("java.util.concurrent.ForkJoinPool.common.exceptionHandler",YourUncaughtExceptionHandler.class.getName());

参数指定

-Djava.util.concurrent.ForkJoinPool.common.exceptionHandler=com.xxx.xxx.YourUncaughtExceptionHandler

WorkQueue

// Mode bits for ForkJoinPool.config and WorkQueue.config
    static final int MODE_MASK    = 0xffff << 16;  // top half of int
    static final int LIFO_QUEUE   = 0;
    static final int FIFO_QUEUE   = 1 << 16;
    static final int SHARED_QUEUE = 1 << 31;       // must be negative

控制是FIFO还是LIFO

   /**
         * Takes next task, if one exists, in order specified by mode.
         */
        final ForkJoinTask<?> nextLocalTask() {
            return (config & FIFO_QUEUE) == 0 ? pop() : poll();
        }

ForkJoinPool 的每个工作线程都维护着一个工作队列(WorkQueue),这是一个双端队列(Deque),里面存放的对象是任务(ForkJoinTask)。

每个工作线程在运行中产生新的任务(通常是因为调用了 fork())时,会放入工作队列的队尾,并且工作线程在处理自己的工作队列时,使用的是 LIFO 方式,也就是说每次从队尾取出任务来执行

每个工作线程在处理自己的工作队列同时,会尝试窃取一个任务(或是来自于刚刚提交到 pool的任务,或是来自于其他工作线程的工作队列),窃取的任务位于其他线程的工作队列的队首,也就是说工作线程在窃取其他工作线程的任务时,使用的是 FIFO 方式。

  • queue capacity

/**
         * Capacity of work-stealing queue array upon initialization.
         * Must be a power of two; at least 4, but should be larger to
         * reduce or eliminate cacheline sharing among queues.
         * Currently, it is much larger, as a partial workaround for
         * the fact that JVMs often place arrays in locations that
         * share GC bookkeeping (especially cardmarks) such that
         * per-write accesses encounter serious memory contention.
         */
        static final int INITIAL_QUEUE_CAPACITY = 1 << 13;

        /**
         * Maximum size for queue arrays. Must be a power of two less
         * than or equal to 1 << (31 - width of array entry) to ensure
         * lack of wraparound of index calculations, but defined to a
         * value a bit less than this to help users trap runaway
         * programs before saturating systems.
         */
        static final int MAXIMUM_QUEUE_CAPACITY = 1 << 26; // 64M

超出报异常

        /**
         * Initializes or doubles the capacity of array. Call either
         * by owner or with lock held -- it is OK for base, but not
         * top, to move while resizings are in progress.
         */
        final ForkJoinTask<?>[] growArray() {
            ForkJoinTask<?>[] oldA = array;
            int size = oldA != null ? oldA.length << 1 : INITIAL_QUEUE_CAPACITY;
            if (size > MAXIMUM_QUEUE_CAPACITY)
                throw new RejectedExecutionException("Queue capacity exceeded");
            int oldMask, t, b;
            ForkJoinTask<?>[] a = array = new ForkJoinTask<?>[size];
            if (oldA != null && (oldMask = oldA.length - 1) >= 0 &&
                (t = top) - (b = base) > 0) {
                int mask = size - 1;
                do { // emulate poll from old array, push to new array
                    ForkJoinTask<?> x;
                    int oldj = ((b & oldMask) << ASHIFT) + ABASE;
                    int j    = ((b &    mask) << ASHIFT) + ABASE;
                    x = (ForkJoinTask<?>)U.getObjectVolatile(oldA, oldj);
                    if (x != null &&
                        U.compareAndSwapObject(oldA, oldj, x, null))
                        U.putObjectVolatile(a, j, x);
                } while (++b != t);
            }
            return a;
        }
PreviousFork/Join框架的实现原理Nextjava.util.concurrent包讲解

Last updated 5 years ago

Was this helpful?