Java program ape will be four thread pool

Preface for Java programs ape, the thread pool is a high-frequency interview questions, we must master a skill, give you on this article uses four main thread pool.

Introduction thread pool

 Thread pool concept:

The thread pool is to first create some threads, their set is called a thread pool. Use the thread pool may well improve performance, the thread pool is to create a large number of idle threads at system startup, the program will pass a task thread pool, thread pool will start a thread to perform this task, after the end of execution, the thread and do not die, but returned again to the thread pool becomes idle, waiting for the next task.

Thread pool mechanism:

         In programming mode thread pool, the task is to be submitted to the whole thread pool instead submitted directly to a thread, the thread pool task in getting it to find out whether there is an idle thread inside, if there are, then the task of the cross to an idle thread.

         A thread can only perform one task at the same time, but you can submit multiple tasks at the same time to a thread pool.

Use the thread pool reasons:

        Multi-threaded runtime, the system constantly new thread startup and shutdown, the cost is very high, the transition will consume system resources, and the risk of transition switching threads, which may lead to the collapse of system resources. In this case, the thread pool is the best choice.

 

Detailed four common thread pool

4.1 Executors.newCacheThreadPool()

Executors.newCacheThreadPool () : cacheable thread pool, the pool has no thread to see previously established, if there is, it is used directly. If not, build a new thread to join the pool, pond cache type commonly used to perform some of the tasks the lifetime of an asynchronous short

Code:

  1. import java.util.concurrent.ExecutorService;  
  2. import java.util.concurrent.Executors;  
  3. public class ThreadPoolExecutorTest {  
  4. public static void main(String[] args) {  
  5. // create a thread pool cacheable  
  6. ExecutorService cachedThreadPool = Executors.newCachedThreadPool();  
  7. for (int i = 0; i < 10; i++) {  
  8. try {  
  9. // sleep can clearly see the inside using a thread pool thread before, did not create a new thread  
  10. Thread.sleep(1000);  
  11. catch (InterruptedException e) {  
  12. e.printStackTrace ();  
  13. }  
  14. cachedThreadPool.execute(new Runnable() {  
  15. public void run() {  
  16. // cache thread is executing Print  
  17. System.out.println (Thread.currentThread () getName () + ". Being executed " );  
  18. }  
  19. });  
  20. }  
  21. }  
  22. }  

Thread pool is infinite, on a task has been completed when the execution of the current task will be a complex task on the thread execution, rather than each time a new thread

4.2 Executors.newFixedThreadPool(int n)

Executors.newFixedThreadPool (n-int) : creates a fixed number of reusable thread pool, shared unbounded queue threads to run.

Code:

  1. package com.study.test;  
  2. import java.util.concurrent.ExecutorService;  
  3. import java.util.concurrent.Executors;  
  4. public class ThreadPoolExecutorTest {  
  5. public static void main(String[] args) {  
  6. // create a reusable fixed number of thread pool  
  7. ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);  
  8. for (int i = 0; i < 10; i++) {  
  9. fixedThreadPool.execute(new Runnable() {  
  10. public void run() {  
  11. try {  
  12. // cache thread is executing Print  
  13. System.out.println (Thread.currentThread () getName () + ". Being executed " );  
  14. Thread.sleep(2000);  
  15. catch (InterruptedException e) {  
  16. e.printStackTrace ();  
  17. }  
  18. }  
  19. });  
  20. }  
  21. }  
  22. }  
  23. }  

 

4.3 Executors.newScheduledThreadPool(int n)

 Executors.newScheduledThreadPool (the n-int) : create a fixed-length thread pool to support regular and periodic task execution

Code:

  1. package com.study.test;  
  2. import java.util.concurrent.Executors;  
  3. import java.util.concurrent.ScheduledExecutorService;  
  4. import java.util.concurrent.TimeUnit;  
  5. public class ThreadPoolExecutorTest {  
  6. public static void main(String[] args) {  
  7. // create a fixed-size thread pool to support regular and periodic task execution - delayed execution  
  8. ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);  
  9. // delay of 1 Miao execution  
  10. scheduledThreadPool.schedule(new Runnable() {  
  11. public void run() {  
  12. System.out.println ( " delay . 1 seconds executed " );  
  13. }  
  14. }, 1, TimeUnit.SECONDS);  
  15. }  
  16. }  

Output Results: Delay . 1 seconds executed

 

Code 2 : Timing can be performed

  1. package com.study.test;  
  2. import java.util.concurrent.Executors;  
  3. import java.util.concurrent.ScheduledExecutorService;  
  4. import java.util.concurrent.TimeUnit;  
  5. public class ThreadPoolExecutorTest {  
  6. public static void main(String[] args) {  
  7. // create a fixed-size thread pool to support regular and periodic task execution - regular basis  
  8. ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);  
  9. // Delay 1 every second after 3 to perform a second  
  10. scheduledThreadPool.scheduleAtFixedRate(new Runnable() {  
  11. public void run() {  
  12. System.out.println ( " Delay 1 after every second . 3 seconds to perform a " );  
  13. }  
  14. }, 1, 3, TimeUnit.SECONDS);  
  15. }  
  16. }  

 

4.4 Executors.newSingleThreadExecutor () 

 Executors.newSingleThreadExecutor () : Creates a single-threaded thread pool, use it only to perform the task only worker threads to ensure that all tasks in a specified order (FIFO, LIFO, priorities ) execution.

  1. package com.study.test;  
  2. import java.util.concurrent.ExecutorService;  
  3. import java.util.concurrent.Executors;  
  4. public class TestThreadPoolExecutor {  
  5. public static void main(String[] args) {  
  6. ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();  
  7. for (int i = 0; i < 10; i++) {  
  8. final int index = i;  
  9. singleThreadExecutor.execute(new Runnable() {  
  10. public void run() {  
  11. try {  
  12. // result is sequentially output, equivalent to the implementation of each task order  
  13. System.out.println (Thread.currentThread () getName () + ". Is being executed , the value of the print is :" + index);  
  14. Thread.sleep(1000);  
  15. catch (InterruptedException e) {  
  16. e.printStackTrace ();  
  17. }  
  18. }  
  19. });  
  20. }  
  21. }  
  22. }  

Guess you like

Origin www.cnblogs.com/gcghcxy/p/11164664.html