Three ways to use the thread pool

Three ways to use the 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?

 

Three use the thread pool

(1) thread pool first use: Executors.newFixedThreadPool ()

package com.wwl.juc;

 

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

 

/**

 * Thread pool used in three ways

 1 * , Executors.newFixedThreadPool (5) for long-term task, and good performance

 2 * , Executors.newSingleThreadExecutor () a task to perform a task, assure an orderly

 3 * , Executors.newCachedThreadPool () for a short asynchronous tasks or very light load service

 */

public class ThreadPoolDemoOne {

    public static void main(String[] args) {

        // a thread pool threads to create five fixed

        ExecutorService threadPool = Executors.newFixedThreadPool(5);

 

        // thread pool first use: 1, Executors.newFixedThreadPool (5)

        try {

            for (int i = 0; i < 10; i++) {

                threadPool.execute(() -> {

                    System.out.println (Thread.currentThread () getName () + "\ t transact business.");

                });

            }

        } catch (Exception e) {

            e.printStackTrace ();

        }

    }

}

Program execution results are as follows: a maximum of only five threads simultaneously perform tasks

(2) The second thread pool use: Executors.newSingleThreadExecutor ()

package com.wwl.juc;

 

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

 

/**

 * Thread pool used in three ways

 1 * , Executors.newFixedThreadPool (5) for long-term task, and good performance

 2 * , Executors.newSingleThreadExecutor () a task to perform a task, assure an orderly

 3 * , Executors.newCachedThreadPool () for a short asynchronous tasks or very light load service

 

 */

public class ThreadPoolDemoTwo {

    public static void main(String[] args) {

        // a thread pool to create a thread

        ExecutorService threadPoolTwo = Executors.newSingleThreadExecutor();

 

        // The second thread pool use: 2, Executors.newSingleThreadExecutor ()

        try {

            for (int i = 0; i < 10; i++) {

                threadPoolTwo.execute(() -> {

                    System.out.println (Thread.currentThread () getName () + "\ t transact business.");

                });

            }

        } catch (Exception e) {

            e.printStackTrace ();

        }

 

    }

}

Program execution results are as follows: only a single thread to perform tasks

(3) The third thread pool use: Executors.newCachedThreadPool ()

 

package com.wwl.juc;

 

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

 

/**

 * Thread pool used in three ways

 1 * , Executors.newFixedThreadPool (5) for long-term task, and good performance

 2 * , Executors.newSingleThreadExecutor () a task to perform a task, assure an orderly

 3 * , Executors.newCachedThreadPool () for a short asynchronous tasks or very light load service

 */

public class ThreadPoolDemoThree {

    public static void main(String[] args) throws InterruptedException {

        // a thread pool threads to create N

        ExecutorService threadPoolThree = Executors.newCachedThreadPool();

 

        // thread pool using a third way: 3, Executors.newCachedThreadPool ()

        for (int i = 0; i < 10; i++) {

            threadPoolThree.execute(() -> {

                System.out.println (Thread.currentThread () getName () + "\ t transact business.");

            });

                     // simulate long-term task, so that the thread execution time becomes longer

//            Thread.sleep(200);

        }

 

    }

}

Program execution results are as follows: Executors.newCachedThreadPool (); suitable for short-term assignments, you can create a pool of threads N threads , but simulating long-term task, there is only one thread of execution.

Short-term simulation task execution:

Simulating long-term task to perform: Thread.sleep (200);

Guess you like

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