多线程学习Day08

ThreadPoolExecutor

1.线程池状态

使用int的高三位来表示线程池的状态,低29位表示数量

 从数字上比较,TERMINATED > TIDYING > STOP > SHUTDOWN > RUNNING(最高位是符号位)

这些信息存储在一个原子变量 ctl 中,目的是将线程池状态与线程个数合二为一,这样就可以用一次 cas 原子操作进行赋值

// c 为旧值, ctlOf 返回结果为新值
ctl.compareAndSet(c, ctlOf(targetState, workerCountOf(c))));
// rs 为高 3 位代表线程池状态, wc 为低 29 位代表线程个数,ctl 是合并它们
private static int ctlOf(int rs, int wc) { return rs | wc; }
 2.构造方法
public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue,
                          ThreadFactory threadFactory,
                          RejectedExecutionHandler handler)

corePoolSize 核心线程数目 (最多保留的线程数)
maximumPoolSize 最大线程数目
keepAliveTime 生存时间 - 针对救急线程
unit 时间单位 - 针对救急线程
workQueue 阻塞队列
threadFactory 线程工厂 - 可以为线程创建时起个好名字
handler 拒绝策略

线程池中刚开始没有线程,当一个任务提交给线程池后,线程池会创建一个新线程来执行任务。
当线程数达到 corePoolSize 并没有线程空闲,这时再加入任务,新加的任务会被加入workQueue 队列排队,直到有空闲的线程。
如果队列选择了有界队列,那么任务超过了队列大小时,会创建 maximumPoolSize -corePoolSize 数目的线程来救急。
如果线程到达 maximumPoolSize 仍然有新任务这时会执行拒绝策略。拒绝策略 jdk 提供了 4 种实现,其它著名框架也提供了实现

        AbortPolicy 让调用者抛出 RejectedExecutionException 异常,这是默认策略

        CallerRunsPolicy 让调用者运行任务
        DiscardPolicy 放弃本次任务
        DiscardOldestPolicy 放弃队列中最早的任务,本任务取而代之


        Dubbo 的实现,在抛出 RejectedExecutionException 异常之前会记录日志,并 dump 线程栈          信息,方便定位问题
        Netty 的实现,是创建一个新线程来执行任务
        ActiveMQ 的实现,带超时等待(60s)尝试放入队列,类似我们之前自定义的拒绝策略
        PinPoint 的实现,它使用了一个拒绝策略链,会逐一尝试策略链中每种拒绝策略

当高峰过去后,超过corePoolSize 的救急线程如果一段时间没有任务做,需要结束节省资源,这个时间由keepAliveTime 和 unit 来控制。根据这个构造方法,JDK Executors 类中提供了众多工厂方法来创建各种用途的线程池

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

特点
核心线程数 == 最大线程数(没有救急线程被创建),因此也无需超时时间
阻塞队列是无界的,可以放任意数量的任务
评价 适用于任务量已知,相对耗时的任务

例子:

@Slf4j(topic = "c.Test2")
public class Test2 {
    public static void main(String[] args) {
        ExecutorService pool= Executors.newFixedThreadPool(2);
        pool.execute(()->{
            log.debug("1");
        });
        pool.execute(()->{
            log.debug("2");
        });
        pool.execute(()->{
            log.debug("3");
        });
    }
}
19:22:37 [pool-1-thread-1] c.Test2 - 1
19:22:37 [pool-1-thread-1] c.Test2 - 3
19:22:37 [pool-1-thread-2] c.Test2 - 2
4.newCachedThreadPool
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                              60L, TimeUnit.SECONDS,
                              new SynchronousQueue<Runnable>());
}

特点
核心线程数是 0, 最大线程数是 Integer.MAX_VALUE,救急线程的空闲生存时间是 60s,意味着
全部都是救急线程(60s 后可以回收)救急线程可以无限创建
队列采用了 SynchronousQueue 实现特点是,它没有容量,没有线程来取是放不进去的(一手交钱、一手交货)

评价 整个线程池表现为线程数会根据任务量不断增长,没有上限,当任务执行完毕,空闲 1分钟后释放线程。 适合任务数比较密集,但每个任务执行时间较短的情况

 SynchronousQueue<Integer> integers = new SynchronousQueue<>();
new Thread(() -> {
        try {
        log.debug("putting {} ", 1);
        integers.put(1);
        log.debug("{} putted...", 1);
        log.debug("putting...{} ", 2);
        integers.put(2);
        log.debug("{} putted...", 2);
        } catch (InterruptedException e) {
        e.printStackTrace();
        }
        },"t1").start();
        sleep(1);
        new Thread(() -> {
        try {
        log.debug("taking {}", 1);
        integers.take();
        } catch (InterruptedException e) {
        e.printStackTrace();
        }
        },"t2").start();
        sleep(1);
        new Thread(() -> {
        try {
        log.debug("taking {}", 2);
        integers.take();
        } catch (InterruptedException e) {
        e.printStackTrace();
        }
        },"t3").start();
5.newSingleThreadExecutor
public static ExecutorService newSingleThreadExecutor() {
         return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
}

使用场景:
希望多个任务排队执行。线程数固定为 1,任务数多于 1 时,会放入无界队列排队。任务执行完毕,这唯一的线程也不会被释放。

区别:
自己创建一个单线程串行执行任务,如果任务执行失败而终止那么没有任何补救措施,而线程池还会新建一个线程,保证池的正常工作

Executors.newSingleThreadExecutor() 线程个数始终为1,不能修改
      FinalizableDelegatedExecutorService 应用的是装饰器模式,只对外暴露了 ExecutorService 接口,因此不能调用 ThreadPoolExecutor 中特有的方法

Executors.newFixedThreadPool(1) 初始时为1,以后还可以修改
      对外暴露的是 ThreadPoolExecutor 对象,可以强转后调用 setCorePoolSize 等方法进行修改

例子:这里线程池并不会由于除以0了而结束

@Slf4j(topic = "c.Test3")
public class Test3 {
    public static void main(String[] args) {
        ExecutorService pool= Executors.newSingleThreadExecutor();
        pool.execute(()->{
            log.debug("1");
            int i=1/0;
        });
        pool.execute(()->{
            log.debug("2");

        });
        pool.execute(()->{
            log.debug("3");
        });
    }
}
19:36:16 [pool-1-thread-1] c.Test3 - 1
19:36:16 [pool-1-thread-2] c.Test3 - 2
19:36:16 [pool-1-thread-2] c.Test3 - 3
Exception in thread "pool-1-thread-1" java.lang.ArithmeticException: / by zero
	at n8.Test3.lambda$main$0(Test3.java:13)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:829)
6.提交任务
void execute(Runnable command);
    // 提交任务 task,用返回值 Future 获得任务执行结果
    <T> Future<T> submit(Callable<T> task);
    // 提交 tasks 中所有任务
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
            throws InterruptedException;
    // 提交 tasks 中所有任务,带超时时间
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
                                  long timeout, TimeUnit unit)
            throws InterruptedException;
    // 提交 tasks 中所有任务,哪个任务先成功执行完毕,返回此任务执行结果,其它任务取消
    <T> T invokeAny(Collection<? extends Callable<T>> tasks)
            throws InterruptedException, ExecutionException;
    // 提交 tasks 中所有任务,哪个任务先成功执行完毕,返回此任务执行结果,其它任务取消,带超时时间
    <T> T invokeAny(Collection<? extends Callable<T>> tasks,
                    long timeout, TimeUnit unit)
            throws InterruptedException, ExecutionException, TimeoutException;

用Future submit的小例子

@Slf4j(topic = "c.TestSubmit")
public class TestSubmit {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService pool= Executors.newFixedThreadPool(2);
        Future<String> future =pool.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                log.debug("running");
                Thread.sleep(1000);
                return "OK";
            }
        });

        log.debug("{}",future.get());
    }
}

invokeAll的小例子

@Slf4j(topic = "c.TestSubmit")
public class TestSubmit {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService pool= Executors.newFixedThreadPool(2);
        List<Future<Object>> futures = pool.invokeAll(Arrays.asList(()->{
            log.debug("begin");
            Thread.sleep(1000);
            return "1";
        },
        ()->{
            log.debug("begin");
            Thread.sleep(500);
            return "2";
        },
        ()->{
            log.debug("begin");
            Thread.sleep(2000);
            return "3";
        }
        ));
        futures.forEach(f->{

            try {
                log.debug("{}",f.get());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }

        });
    }

结果

21:32:04 [pool-1-thread-2] c.TestSubmit - begin
21:32:04 [pool-1-thread-1] c.TestSubmit - begin
21:32:04 [pool-1-thread-2] c.TestSubmit - begin
21:32:06 [main] c.TestSubmit - 1
21:32:06 [main] c.TestSubmit - 2
21:32:06 [main] c.TestSubmit - 3

invokeAny的小例子

@Slf4j(topic = "c.TestSubmit")
public class TestSubmit {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService pool= Executors.newFixedThreadPool(3);
        String result = pool.invokeAny(Arrays.asList(()->{
            log.debug("begin");
            Thread.sleep(1000);
            log.debug("end");
            return "1";
        },
        ()->{
            log.debug("begin");
            Thread.sleep(500);
            log.debug("end");
            return "2";
        },
        ()->{
            log.debug("begin");
            Thread.sleep(2000);
            log.debug("end");
            return "3";
        }
        ));

    }
7.关闭线程池

shutdown,线程池状态变为 SHUTDOWN- 不会接收新任务- 但已提交任务会执行完- 此方法不会阻塞调用线程的执行

 public void shutdown() {
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            checkShutdownAccess();
// 修改线程池状态
            advanceRunState(SHUTDOWN);
// 仅会打断空闲线程
            interruptIdleWorkers();
            onShutdown(); // 扩展点 ScheduledThreadPoolExecutor
        } finally {
            mainLock.unlock();
        }
// 尝试终结(没有运行的线程可以立刻终结,如果还有运行的线程也不会等)
        tryTerminate();
    }

shutdownNow,线程池状态变为 STOP- 不会接收新任务- 会将队列中的任务返回- 并用 interrupt 的方式中断正在执行的任务

 public List<Runnable> shutdownNow() {
        List<Runnable> tasks;
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            checkShutdownAccess();
// 修改线程池状态
            advanceRunState(STOP);
// 打断所有线程
            interruptWorkers();
// 获取队列中剩余任务
            tasks = drainQueue();
        } finally {
            mainLock.unlock();
        }
// 尝试终结
        tryTerminate();
        return tasks;
    }
    }

其他方法

// 不在 RUNNING 状态的线程池,此方法就返回 true
boolean isShutdown();
// 线程池状态是否是 TERMINATED
boolean isTerminated();
// 调用 shutdown 后,由于调用线程并不会等待所有任务运行结束,因此如果它想在线程池 TERMINATED 后做些事
情,可以利用此方法等待
boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;

异步模式之工作线程

定义

让有限的工作线程(Worker Thread)来轮流异步处理无限多的任务。也可以将其归类为分工模式,它的典型实现就是线程池,也体现了经典设计模式中的享元模式。

例如,海底捞的服务员(线程),轮流处理每位客人的点餐(任务),如果为每位客人都配一名专属的服务员,那么成本就太高了(对比另一种多线程设计模式:Thread-Per-Message)

注意,不同任务类型应该使用不同的线程池,这样能够避免饥饿,并能提升效率
例如,如果一个餐馆的工人既要招呼客人(任务类型A),又要到后厨做菜(任务类型B)显然效率不咋地,分成服务员(线程池A)与厨师(线程池B)更为合理,当然可以有更细致的分工

饥饿

固定大小线程池会有饥饿现象
两个工人是同一个线程池中的两个线程
他们要做的事情是:为客人点餐和到后厨做菜,这是两个阶段的工作
     客人点餐:必须先点完餐,等菜做好,上菜,在此期间处理点餐的工人必须等待
     后厨做菜:没啥说的,做就是了
比如工人A 处理了点餐任务,接下来它要等着 工人B 把菜做好,然后上菜,他俩也配合的蛮好
但现在同时来了两个客人,这个时候工人A 和工人B 都去处理点餐了,这时没人做饭了,饥饿

比如这个

@Slf4j(topic = "c.TestDeadLock")
public class TestDeadLock {
    static final List<String> MENU = Arrays.asList("地三鲜", "宫保鸡丁", "辣子鸡丁", "烤鸡翅");
    static Random RANDOM = new Random();
    static String cooking() {
        return MENU.get(RANDOM.nextInt(MENU.size()));
    }
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.execute(() -> {
            log.debug("处理点餐...");
            Future<String> f = executorService.submit(() -> {
                log.debug("做菜");
                return cooking();
            });
            try {
                log.debug("上菜: {}", f.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });
executorService.execute(() -> {
log.debug("处理点餐...");
Future<String> f = executorService.submit(() -> {
log.debug("做菜");
return cooking();
});
try {
log.debug("上菜: {}", f.get());
} catch (InterruptedException | ExecutionException e) {


        e.printStackTrace();
    }
});
        }
}

两个线程池解决问题

@Slf4j(topic = "c.TestDeadLock")
public class TestDeadLock {
    static final List<String> MENU = Arrays.asList("地三鲜", "宫保鸡丁", "辣子鸡丁", "烤鸡翅");
    static Random RANDOM = new Random();
    static String cooking() {
        return MENU.get(RANDOM.nextInt(MENU.size()));
    }
    public static void main(String[] args) {
        ExecutorService waiterPool = Executors.newFixedThreadPool(2);
        ExecutorService cookerPool = Executors.newFixedThreadPool(2);
        waiterPool.execute(() -> {
            log.debug("处理点餐...");
            Future<String> f = cookerPool.submit(() -> {
                log.debug("做菜");
                return cooking();
            });
            try {
                log.debug("上菜: {}", f.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });
        waiterPool.execute(() -> {
            log.debug("处理点餐...");
            Future<String> f = cookerPool.submit(() -> {
            log.debug("做菜");
            return cooking();
          });
          try {
          log.debug("上菜: {}", f.get());
          } catch (InterruptedException | ExecutionException e) {


        e.printStackTrace();
    }
});
        }
}
线程池大小

过小会导致程序不能充分地利用系统资源、容易导致饥饿
过大会导致更多的线程上下文切换,占用更多内存

CPU 密集型运算

通常采用 cpu 核数 + 1 能够实现最优的 CPU 利用率,+1 是保证当线程由于页缺失故障(操作系统)或其它原因导致暂停时,额外的这个线程就能顶上去,保证 CPU 时钟周期不被浪费

I/O 密集型运算

CPU 不总是处于繁忙状态,例如,当你执行业务计算时,这时候会使用 CPU 资源,但当你执行 I/O 操作时、远程RPC 调用时,包括进行数据库操作时,这时候 CPU 就闲下来了,你可以利用多线程提高它的利用率。

经验公式如下
线程数 = 核数 * 期望 CPU 利用率 * 总时间(CPU计算时间+等待时间) / CPU 计算时间
例如 4 核 CPU 计算时间是 50% ,其它等待时间是 50%,期望 cpu 被 100% 利用,套用公式
4 * 100% * 100% / 50% = 8
例如 4 核 CPU 计算时间是 10% ,其它等待时间是 90%,期望 cpu 被 100% 利用,套用公式
4 * 100% * 100% / 10% = 40

8.任务调度线程池

在『任务调度线程池』功能加入之前,可以使用 java.util.Timer 来实现定时功能,Timer 的优点在于简单易用,但由于所有任务都是由同一个线程来调度,因此所有任务都是串行执行的,同一时间只能有一个任务在执行,前一个任务的延迟或异常都将会影响到之后的任务。

一些例子

@Slf4j(topic = "c.Test4")
public class Test4 {
    public static void main(String[] args) {
        ScheduledExecutorService pool= Executors.newScheduledThreadPool(2);
        log.debug("start...");
//        pool.scheduleAtFixedRate(()->{
//            log.debug("running");
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//        },1,1,TimeUnit.SECONDS);
        pool.scheduleWithFixedDelay(()-> {
            log.debug("running");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },1,1,TimeUnit.SECONDS);
        pool.schedule(()->{
            log.debug("task1");
            int t=1/0;
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },1, TimeUnit.SECONDS);
        pool.schedule(()->{
            log.debug("task2");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },1, TimeUnit.SECONDS);
    }
}

9.处理任务异常

方法1:主动捉异常

ExecutorService pool = Executors.newFixedThreadPool(1);
pool.submit(() -> {
try {
log.debug("task1");
int i = 1 / 0;
} catch (Exception e) {
log.error("error:", e);
}
});

方法2:使用 Future

ExecutorService pool = Executors.newFixedThreadPool(1);
Future<Boolean> f = pool.submit(() -> {
log.debug("task1");
int i = 1 / 0;
return true;
});
log.debug("result:{}", f.get());

应用之定时任务

//让每周四18:00:00定时提交任务
@Slf4j(topic = "c.TestSchedule")
public class TestSchedule {
    public static void main(String[] args) {
        //获取当前时间
        LocalDateTime now=LocalDateTime.now();
        log.debug(String.valueOf(now));
        //获取周四时间
        LocalDateTime time= now.withHour(18).withMinute(0).withSecond(0).withNano(0).with(DayOfWeek.THURSDAY);
        //如果当前时间大于本周周四,必须找下周四
        if(now.compareTo(time)>0){
            time.plusWeeks(1);

        }
        log.debug(String.valueOf(time));
        long initaiLDelay= Duration.between(now,time).toMillis();

        //initailDelay代表当前时间和周四的时间差
        //period一周的间隔时间
        long period=1000*60*60*24*7;
        ScheduledExecutorService pool= Executors.newScheduledThreadPool(1);
        pool.scheduleAtFixedRate(()->{
            log.debug("runing...");
        },initaiLDelay,period, TimeUnit.MICROSECONDS);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值