JUC整理(三)

阻塞队列 BlockingQueue

方式抛出异常有返回值阻塞等待超时等待
添加addofferputoffer(,)
移除removepolltakepoll(,)
判断队列首elment
  1. 抛出异常 出现异常直接抛出
  public static void test1(){
        //初始化是写入最大容量
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);
        System.out.println(arrayBlockingQueue.add("a"));
        System.out.println(arrayBlockingQueue.add("b"));
        System.out.println(arrayBlockingQueue.add("c"));
        //System.out.println(arrayBlockingQueue.add("d"));
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
    }
  1. 有返回值 不会抛出异常
 public static void test2(){
        //初始化是写入最大容量
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);
        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("b"));
        System.out.println(arrayBlockingQueue.offer("c"));

        //System.out.println(arrayBlockingQueue.add("d"));
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
    }
  1. 阻塞等待
   public static void test3() throws InterruptedException {
        //初始化是写入最大容量
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);
        arrayBlockingQueue.put("a");
        arrayBlockingQueue.put("b");
        arrayBlockingQueue.put("c");

        //System.out.println(arrayBlockingQueue.add("d"));
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
    }
  1. 超时等待 超过设置的时间就会取消等待
  public static void test4() throws InterruptedException {
        //初始化是写入最大容量
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);
        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("b"));
        System.out.println(arrayBlockingQueue.offer("c"));

        //System.out.println(arrayBlockingQueue.add("d"));
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll(2, TimeUnit.SECONDS));
    }

同步队列 SynchronousQueue

  • 没有容量 只能放进去一个元素取出后才可以放入
 public static void main(String[] args) {
        SynchronousQueue<String> synchronousQueue = new SynchronousQueue<>();
        new Thread(()->{
            try {
                System.out.println("1111put");
                synchronousQueue.put("11");
                System.out.println("2222put");
                synchronousQueue.put("22");
                System.out.println("3333put");
                synchronousQueue.put("33");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"1111").start();
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
                synchronousQueue.take();
                System.out.println(111);
                TimeUnit.SECONDS.sleep(2);
                synchronousQueue.take();
                System.out.println(222);
                TimeUnit.SECONDS.sleep(2);
                synchronousQueue.take();
                System.out.println(333);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

线程池

  • 降低资源的消耗 提高响应的速度(不用新建和销毁)

  • 方便管理

public ThreadPoolExecutor(int corePoolSize,//核心线程数
                              int maximumPoolSize,//最大核心线程数
                              long keepAliveTime,//超时时间
                              TimeUnit unit,//超时时间单位
                              BlockingQueue<Runnable> workQueue,//阻塞队列
                              ThreadFactory threadFactory,
                          //线程工厂,创建线程的
                              RejectedExecutionHandler handler//拒绝策略
                         ) {
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

线程池的创建

  public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2,
                5,
                3, 
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }

线程池的四种拒绝策略

最大线程如何定义

  • CPU密集型
    • 几核CPU就定义为几可以保持CPU的效率最高
//获取CPU的核数
Runtime.getRuntime().availableProcessors();
  • IO密集型
    • 判断程序中十分消耗IO的线程
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值