• Java线程池 / Executor / Callable / Future


    为什么需要线程池?
     
    每次都要new一个thread,开销大,性能差;不能统一管理;功能少(没有定时执行、中断等)。
     
    使用线程池的好处是,可重用,可管理。
     

    Executor
     
     
    4种线程池
     
     
    // 可缓存线程池,如果缓存中没有可用的,则移出60秒未使用过的线程
    ExecutorService service= Executors.newCachedThreadPool();
     
    // 大小固定的线程池
    ExecutorService service= Executors.newFixedThreadPool(5);
     
    // 单线程,是线程量=1的FixedThreadPool,多任务下相当于排队。
    ExecutorService service= Executors.newSingleThreadPool();
     
    // 支持定时和周期,一般情况下可替代timer
    ScheduledExecutorService exec = Executors.newScheduledThreadPool(int corePoolSize)
     
     
    Demo
     
     
    ExecutorService pool= Executors.newCachedThredPool();
    pool.ececute(new Runable());
     
    // 关闭,不再接受新的任务请求
    pool.shutdown();
     
    // 立即关闭,停止接受task,
    pool.showdownNow();
     
     
    Future
     
    .submit(Runnable) 返回一个Future,主要目的是检查任务执行的状况(是否完成,有无异常)。
     
    interface Future<V>
    {
         V get() throws ...;
         V get(long timeout,TimeUnit unit) throws ..;
         void cancel(boolean mayInterrupt); // 取消任务,如果已经开始,mayInterrupt=true时被中断
         boolean isCancelled();
         boolean isDown();
    }
     
    Future task = pool.submit(new Runnable());
    task.isDone(); // 
     
    task.get(); // 阻塞当前线程,如果task在执行过程中有异常,则会在这里重新抛出
     
     
    Callable
     
    Runnable没有返回值,Callable<E>有返回值;
    submit一个runnable,不能知道运行结果,可以submit一个callable。
     
    // 创建callable
    class MyCallable implements Callable<Integer>
    {
         @Override
         public Integer call()
         {
              return 1;
         }
    }
     
    Future<Integer> task = pool.submit(new MyCallable());
     
    task.get(); // 阻塞,显示结果
     
     
    FutureTask
     
    同时包装了Future和Task。
    Callable<Integer> c = ...;
    FutureTask<Integer> t = new FutureTask<Integer>(c);
    new Thread(task).start();
    Integer r = t.get();
     
     
    CompletionService
     
    completionService = new ExecutorCompletionService<Long>(exec);
    for(){
          completionService.submit(new Callable());
    }
    // 取完成的任务,如果没有,就阻塞等待
    completionService.take().get()
     
  • 相关阅读:
    log4net 简单使用教程(配置)
    C#WinForm 国际化的简单实现,多语言实现
    EF Power Tool 参数错误 HRESULT:0x80070057 (E_INVALIDARG)) 解决办法
    如何用委托与事件实现winfrom窗体之间值传递
    VS2010自带的Dotfuscator 5注册
    WinForm 实现主程序(exe)与其关联类库(*.dll)分开存放
    POJ_3211 Washing Clothes (01背包)
    POJ_3624 Charm Bracelet ( 01背包 )
    集训队内部测试赛(2012.01.02)
    HDU_1011 Starship Troopers && HDU_1561 The more, The Better (树型dp)
  • 原文地址:https://www.cnblogs.com/caca/p/java_executors.html
Copyright © 2020-2023  润新知