• 2.1新建线程


    public class 自定义线程创建 {
    public static void main(String[] args) {
    ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(2,
    5,
    0,
    TimeUnit.SECONDS,
    new ArrayBlockingQueue<Runnable>(2),
    new ThreadFactory() {
    public Thread newThread(Runnable r) {
    Thread t=new Thread(r);
    System.out.println("Creat");
    return t;
    }
    },
    // new ThreadPoolExecutor.AbortPolicy());//第一种拒绝策略:直接抛出异常AbortPolicy
    new ThreadPoolExecutor.CallerRunsPolicy());//第二种拒绝策略:新建一个线程
    // new ThreadPoolExecutor.DiscardOldestPolicy());//第三种拒绝策略:在队列里推出一个最早的
    // new ThreadPoolExecutor.DiscardPolicy());//第四种:直接丢弃。
    // new RejectedExecutionHandler() {
    // public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
    // System.out.println(Thread.currentThread().getName() + "被抛弃了");
    // try {
    // Thread.sleep(2000);
    // } catch (InterruptedException e) {
    // e.printStackTrace();
    // }
    // System.out.println(Thread.currentThread().getName() + "拒绝成功");
    //
    // }
    // });//第种:直接丢弃。
    for (int i = 0; i < 10; i++) {
    final int finalI = i;
    threadPoolExecutor.execute(new Runnable() {
    public void run() {
    System.out.println(Thread.currentThread().getName()+"开始"+ finalI);
    try {
    Thread.sleep(10000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName()+"结束"+ finalI); }
    });
    }
    }
    }
  • 相关阅读:
    [转载]三小时学会Kubernetes:容器编排详细指南
    《微服务设计》书摘
    Mac os安装golang开发环境
    openstack-KVM-Network
    openstack-KVM管理工具
    openstack-KVM-vCPU
    openstack-KVM-Memory
    openstack-KVM-存储配置
    openstack-云计算概述
    openstack-KVM安装与使用
  • 原文地址:https://www.cnblogs.com/anxbb/p/8425321.html
Copyright © 2020-2023  润新知