Seven parameters introduced thread pool

Seven parameters introduced thread pool

Manufacturers face questions:

1, you talk about the understanding of the volatile?

2, CAS Did you know?

3, ABA Atomic AtomicInteger class to talk about? Atomic update references know?

4, we all know that ArrayList is not thread-safe, please write an insecure coding case and gives the solution?

5, lock fair / unfair lock / reentrant lock / recursive lock / spin locks talk about your understanding? Please handwriting a spin lock.

6, CountDownLatch, CyclicBarrier, Semaphore used it?

7, blocking queue know?

8 , the thread pool used it? ThreadPoolExecutor talk about your understanding?

9, the thread pool used it? Production How do you set reasonable parameters?

10, location analysis and coding deadlock?

 

1, entry thread pool of seven parameters

first step:

ExecutorService threadPool = Executors.newFixedThreadPool(5);

 

Step two:

    public static ExecutorService newFixedThreadPool(int nThreads) {

        return new ThreadPoolExecutor(nThreads, nThreads,

                                      0L, TimeUnit.MILLISECONDS,

                                      new LinkedBlockingQueue<Runnable>());

    }

 

third step:

    public ThreadPoolExecutor(int corePoolSize,

                              int maximumPoolSize,

                              long keepAliveTime,

                              TimeUnit unit,

                              BlockingQueue<Runnable> workQueue) {

        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,

             Executors.defaultThreadFactory(), defaultHandler);

 

the fourth step:

    public ThreadPoolExecutor(int corePoolSize,

                              int maximumPoolSize,

                              long keepAliveTime,

                              TimeUnit unit,

                              BlockingQueue<Runnable> workQueue,

                              ThreadFactory threadFactory,

                              RejectedExecutionHandler handler) {

        if (corePoolSize < 0 ||

            maximumPoolSize <= 0 ||

            maximumPoolSize < corePoolSize ||

            keepAliveTime < 0)

            throw new IllegalArgumentException();

        if (workQueue == null || threadFactory == null || handler == null)

            throw new NullPointerException();

        this.acc = System.getSecurityManager() == null ?

                null :

                AccessController.getContext();

        this.corePoolSize = corePoolSize;

        this.maximumPoolSize = maximumPoolSize;

        this.workQueue = workQueue;

        this.keepAliveTime = unit.toNanos(keepAliveTime);

        this.threadFactory = threadFactory;

        this.handler = handler;

    }

 

2, the thread pool parameters introduced seven

(1) corePoolSize: Permanent core number of threads in the thread pool

(2) maximumPoolSize: thread pool can accommodate the maximum number of execution threads, this value must be greater than or equal to 1

(3) keepAliveTime: excess idle threads survival time. When the current number of more than corePoolSize thread pool, when the idle time reaches keepAliveTime value, the excess idle threads will be destroyed until only so far corePoolSize threads.

(4) unit: keepAliveTime time unit

(5) workQueue: task queue, the task is submitted but not yet implemented

(6) threadFactory: presentation generator thread pool worker thread factory used to create a thread, usually the default thread factory can

(7) handler: deny policy, means that when the queue is full and how the worker threads greater than the maximum number of threads equal to the thread pool (maximumPoolSize) to refuse to request the Runnable strategy

Guess you like

Origin blog.csdn.net/longgeqiaojie304/article/details/93475626