• ExecutorService,另一种服务,线程


    http://heipark.iteye.com/blog/1393847

    Executors.newFixedThreadPool和ArrayBlockingQueue一点使用心得          

      博客分类:
    • Java
     

    newFixedThreadPool使用范例:

    Java代码 复制代码 收藏代码
    1. import java.io.IOException;  
    2. import java.util.concurrent.ExecutorService;  
    3. import java.util.concurrent.Executors;  
    4.   
    5. public class Test {  
    6.   
    7.     public static void main(String[] args) throws IOException, InterruptedException {  
    8.         ExecutorService service = Executors.newFixedThreadPool(2);  
    9.         for (int i = 0; i < 6; i++) {  
    10.             final int index = i;  
    11.             System.out.println("task: " + (i+1));  
    12.             Runnable run = new Runnable() {  
    13.                 @Override  
    14.                 public void run() {  
    15.                     System.out.println("thread start" + index);  
    16.                     try {  
    17.                         Thread.sleep(Long.MAX_VALUE);  
    18.                     } catch (InterruptedException e) {  
    19.                         e.printStackTrace();  
    20.                     }  
    21.                     System.out.println("thread end" + index);  
    22.                 }  
    23.             };  
    24.             service.execute(run);  
    25.         }  
    26.     }  
    27. }  
     输出:
    task: 1
    task: 2
    thread start0
    task: 3
    task: 4
    task: 5
    task: 6
    task: 7
    thread start1
    task: 8
    task: 9
    task: 10
    task: 11
    task: 12
    task: 13
    task: 14
    task: 15

        从实例可以看到for循环并没有被固定的线程池阻塞住,也就是说所有的线程task都被提交到了ExecutorService中,查看 Executors.newFixedThreadPool()如下:

    public static ExecutorService newFixedThreadPool(int nThreads) {
            return new ThreadPoolExecutor(nThreads, nThreads,
                                          0L, TimeUnit.MILLISECONDS,
                                          new LinkedBlockingQueue<Runnable>());
        }

        可以看到task被提交都了LinkedBlockingQueue中。这里有个问题,如果任务列表很大,一定会把内存撑爆,如何解决?看下面:

    Java代码 复制代码 收藏代码
    1. import java.io.IOException;  
    2. import java.util.concurrent.ArrayBlockingQueue;  
    3. import java.util.concurrent.BlockingQueue;  
    4. import java.util.concurrent.ThreadPoolExecutor;  
    5. import java.util.concurrent.TimeUnit;  
    6.   
    7. public class Test {  
    8.   
    9.     public static void main(String[] args) throws IOException, InterruptedException {  
    10.           
    11.         BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(3);  
    12.           
    13.         ThreadPoolExecutor executor = new ThreadPoolExecutor(331, TimeUnit.HOURS, queue, new ThreadPoolExecutor.CallerRunsPolicy());  
    14.           
    15.         for (int i = 0; i < 10; i++) {  
    16.             final int index = i;  
    17.             System.out.println("task: " + (index+1));  
    18.             Runnable run = new Runnable() {  
    19.                 @Override  
    20.                 public void run() {  
    21.                     System.out.println("thread start" + (index+1));  
    22.                     try {  
    23.                         Thread.sleep(Long.MAX_VALUE);  
    24.                     } catch (InterruptedException e) {  
    25.                         e.printStackTrace();  
    26.                     }  
    27.                     System.out.println("thread end" + (index+1));  
    28.                 }  
    29.             };  
    30.             executor.execute(run);  
    31.         }  
    32.     }  
    33. }  
     输出:
    task: 1
    task: 2
    thread start1
    task: 3
    task: 4
    task: 5
    task: 6
    task: 7
    thread start2
    thread start7
    thread start6

        线程池最大值为4(??这里我不明白为什么是设置值+1,即3+1,而不是3),准备执行的任务队列为3。可以看到for循环先处理4个task,然后把3个放到队列。这样就实现了自动阻塞队列的效果。记得要使用ArrayBlockingQueue这个队列,然后设置容量就OK了。

    --heipark

  • 相关阅读:
    Laravel 5.5 创建全局公共函数
    Mysql性能优化四:分库,分区,分表,你们如何做?
    Mysql性能优化三:主从配置,读写分离
    Mysql性能优化二:索引优化
    Mysql性能优化一:SQL语句性能优化
    Windows Server 2008 R2(x64) IIS7+PHP5(FastCGI)环境搭建
    centos7使用Gogs搭建Git服务器
    CentOS虚拟机和物理机共享文件夹实现
    Flask目录
    MySQL目录
  • 原文地址:https://www.cnblogs.com/melons/p/5791951.html
Copyright © 2020-2023  润新知