当前位置: 首页 > wzjs >正文

做缓网站网推

做缓网站,网推,用jsp源码做网站,贸易公司寮步网站建设价钱👋hi,我不是一名外包公司的员工,也不会偷吃茶水间的零食,我的梦想是能写高端CRUD 🔥 2025本人正在沉淀中… 博客更新速度 👍 欢迎点赞、收藏、关注,跟上我的更新节奏 📚欢迎订阅专栏…

👋hi,我不是一名外包公司的员工,也不会偷吃茶水间的零食,我的梦想是能写高端CRUD

🔥 2025本人正在沉淀中… 博客更新速度++

👍 欢迎点赞、收藏、关注,跟上我的更新节奏

📚欢迎订阅专栏,专栏名《在2B工作中寻求并发是否搞错了什么》

前言

你是否在线程池工具类里看到过它的身影?

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

你是否会好奇LinkedBlockingQueue是啥呢?

没有关系,小手手点上关注,跟上主播的节奏。

什么是LinkedBlockingQueue?

LinkedBlockingQueue 是一个基于链表的线程安全阻塞队列,常用于生产者-消费者模式。

数据结构

  • 基于单向链表实现,队列头尾分别通过 head 和 last 指针维护。
  • 默认容量为 Integer.MAX_VALUE(近似无界队列),但可手动指定固定容量。

线程安全

  • 使用两把锁分离设计(入队锁 putLock 和出队锁 takeLock),提高并发性能。
  • 通过 ReentrantLock 和 Condition 实现阻塞(队列空时阻塞消费者,队列满时阻塞生产者)。

阻塞操作

  • put():队列满时阻塞生产者线程。
  • take():队列空时阻塞消费者线程。
  • 非阻塞方法:offer()(失败返回 false)、poll()(失败返回 null)

简单说说,和我们之前说的ArrayBlokcingQueue的区别:

特性LinkedBlockingQueueArrayBlockingQueue
底层结构链表数组
默认容量Integer.MAX_VALUE(无界)必须显式指定
锁机制双锁分离(更高并发)单锁控制
内存占用动态扩展(链表节点开销)预分配连续内存

简单使用LinkedBlockingQueue

因为我们的LinkedBlockingQueue也是实现了BlockingQueue的接口,所以下面的代码例子,会有这些方法。

在这里插入图片描述

构造方法

首先从构造方法说起吧,LinkedBlockingQueue有3个构造方法:

// 没有传任何参数,默认容量大小为Integer.MAX_VALUE
public LinkedBlockingQueue();// 容量大小为入参
public LinkedBlockingQueue(int capacity)// 容量大小为Integer.MAX_VALUE,集合中初始化元素为c
public LinkedBlockingQueue(Collection<? extends E> c)

添加元素入队操作

add(E e)方法:简单粗暴,非阻塞添加元素,队列满了的话直接抛IllegalStateException异常。

public static void main(String[] args) {BlockingQueue<Object> queue = new LinkedBlockingQueue<>(1);queue.add("A");queue.add("B"); // Exception in thread "main" java.lang.IllegalStateException: Queue full
}

offer(E e)方法:非阻塞添加元素,成功返回boolean,添加成功为true,添加失败为false

public static void main(String[] args) {BlockingQueue<Object> queue = new LinkedBlockingQueue<>(1);boolean result = queue.offer("A"); // trueSystem.out.println(result);result = queue.offer("B"); // falseSystem.out.println(result);
}

offer(E e, long timeout, TimeUnit unit)方法: 向队列添加元素。如果队列满了,就阻塞线程,等待一段时间,一段时间过后队列还是满的话,意味添加元素失败返回false,否则返回true。

public static void main(String[] args) throws InterruptedException {BlockingQueue<Object> queue = new LinkedBlockingQueue<>(1);boolean result = queue.offer("A", 1, TimeUnit.SECONDS); // trueSystem.out.println(result);result = queue.offer("B", 1, TimeUnit.SECONDS); // falseSystem.out.println(result);
}

但如果我们这样等待一会的话,执行结果就会不一样:

public static void main(String[] args) throws InterruptedException {BlockingQueue<Object> queue = new LinkedBlockingQueue<>(1);boolean result = queue.offer("A", 1, TimeUnit.SECONDS); // trueSystem.out.println(result);// 启动另一个生产者线程new Thread(() -> {Boolean res;try {res = queue.offer("B", 2, TimeUnit.SECONDS); // 队列满了,阻塞2s等待} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(res);       // true}).start();// 移除队列中的元素queue.poll();
}

put(E e)方法: 阻塞线程,直到队列不为空或中断者线程。

public static void main(String[] args) throws InterruptedException {BlockingQueue<Object> queue = new LinkedBlockingQueue<>(1);queue.put("A");queue.put("B"); // 队列已满,线程阻塞在这
}

移除元素出队操作

remove()方法:如果队列为空,直接抛出NoSuchElementException异常。

public static void main(String[] args) {BlockingQueue<Object> queue = new LinkedBlockingQueue<>(1);queue.remove(); // Exception in thread "main" java.util.NoSuchElementException
}

poll()方法:非阻塞获取队列头元素,如果队列为空,直接返回null。

public static void main(String[] args) {BlockingQueue<String> queue = new LinkedBlockingQueue<>(1);String poll = queue.poll();System.out.println(poll); // null(队列中没有元素)queue.offer("A");      // 向队列中添加元素poll= queue.poll();System.out.println(poll); // A
}

poll(long timeout, TimeUnit unit)方法:阻塞一段时间获取队列中的元素,如果超过时间了,就队列还是为空,就返回null。

public static void main(String[] args) throws InterruptedException {BlockingQueue<String> queue = new LinkedBlockingQueue<>(1);// 创建一个线程,3s后生产1个元素到队列中new Thread(() -> {try {Thread.sleep(2000);queue.offer("A");} catch (InterruptedException e) {e.printStackTrace();}}).start();// 消费者,阻塞5s获取元素String poll= queue.poll(5 , TimeUnit.SECONDS);System.out.println(poll); // A
}

take()方法:阻塞线程获取队列中的元素,直到队列不为空,或者被其他线程中断,抛出异常停止。

public static void main(String[] args) throws InterruptedException {BlockingQueue<String> queue = new LinkedBlockingQueue<>(1);// 创建一个消费者线程,获取队列中的元素Thread consumerThread = new Thread(() -> {try {String take = queue.take();     // 在这个案例中,线程会被一直阻塞到这,直到被中断System.out.println("消费者线程获取到元素:" + take);} catch (InterruptedException e) {System.out.println("线程者线程被中断了");}});consumerThread.start();// 隔3s后,中断消费者线程TimeUnit.SECONDS.sleep(1);consumerThread.interrupt();
}输出结果:
线程者线程被中断了

检查队列中的元素

element() 方法: 返回队列头的元素,但是如果队列为空,element方法会抛出NoSuchElementException异常。

public static void main(String[] args) throws InterruptedException {BlockingQueue<String> queue = new LinkedBlockingQueue<>(2);queue.offer("A");queue.offer("B");System.out.println(queue.element());    // Aqueue.poll();   // 头元素出队System.out.println(queue.element());    // B
}// 异常的情况
public static void main(String[] args) throws InterruptedException {BlockingQueue<String> queue = new LinkedBlockingQueue<>(2);String element = queue.element();   // Exception in thread "main" java.util.NoSuchElementException
}

peek() 方法:和element()方法差不多,返回队列头的元素,如果队列为空,会返回null

public static void main(String[] args) throws InterruptedException {BlockingQueue<String> queue = new LinkedBlockingQueue<>(2);String element = queue.peek();System.out.println(element);    // null
}

后话

这就结束了?没有的,宝贝,没有的。

这里只是简答的使用,小手手点上关注,主播下一篇,直接开始看LinkedBlockingQueue源码。

http://www.dtcms.com/wzjs/384618.html

相关文章:

  • 国外网站问题软文营销的经典案例
  • 网站建设方案书的内容网络推广公司哪家好
  • 快速做网站的方法厨师培训学校
  • wordpress菜单小图标关于进一步优化
  • wordpress开发的主流架构搜狗seo刷排名软件
  • 烟台网站制作步骤直通车怎么开
  • wordpress获取分类的文章列表seo排名工具给您好的建议下载官网
  • 海丰网站建设百度搜索指数在线查询
  • 网站开发如何运用form表单网络广告策划书模板范文
  • 简单旅游网站模板下载网络推广和网络营销的区别
  • 服务平台入口win10系统优化软件哪个好
  • 视频一页网站怎么做免费发布广告信息平台
  • 北京做电商网站设计营销网页设计公司
  • 模板网站是什么意思游戏推广怎么快速拉人
  • 番禺网站建设外包西安seo按天收费
  • 教育网站建设市场分析计划书杭州百度
  • 网站站群建设重庆百度小额贷款有限公司
  • 小公司让我用织梦做网站网上推广赚钱方法
  • 义乌门户网站建设营销策划案
  • 描述一下网站建设的基本流程百度极速版下载安装
  • 网站建设收费详情微信公众号推广网站
  • 网站建设邀标方案百度广告费
  • 网站改版的几个建议网站推广公司黄页
  • 推荐盐城网站建设seo排名赚挂机
  • 做网站营销公司有哪些网推项目平台
  • wordpress 图片网站舆情监测系统排名
  • 商城网站设计教程网站排名优化软件哪家好
  • 寺庙网站开发文案网站优化排名
  • 商会信息平台网站建设方案爱站网官网
  • 广东省建设信息网站公司注册流程