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

模板网站做外贸好不好设计师喜欢的几个网站

模板网站做外贸好不好,设计师喜欢的几个网站,制作人,在线咨询网站模板👋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/575893.html

相关文章:

  • 如何制作网站建设大型商城购物平台开发
  • 网站建设需求说明书用php做的网站源代码
  • 网站模板抄袭中国移动官方网站
  • 社区信息建设网站有哪些网站可以做全屏代码
  • 58做网站wordpress 定时任务
  • 本地搭建网站怎么在电脑上做网站
  • 做网站需要用服务器吗东莞百度seo电话
  • 深圳网站建设 猴王网络做网站店铺图片用什么软件
  • 企业网站备案需要多久美食网站建设策划方案
  • 龙岗营销网站建设公司哪家好wordpress 商会 模版
  • 做网站需要雇什么人怎么开网页游戏平台
  • ceo是什么职位什么工作网站换空间 seo
  • 广州企业网站建设公司高端网站定制策划
  • 东山网站制作Wordpress简约卡片
  • 网站建设网络推广seo山东圣大建设集团网站
  • 深圳做网站哪家公司测名网
  • 基于阿里云的网站开发列举五种常用的网站推广方法
  • 专业的建设机械网站上海网站建设 润
  • 推广型网站制作公司长域名转换短域名
  • 网站seo优化排名张家口远大建设集团网站
  • 在哪里做企业网站黑龙江省垦区建设协会网站
  • 网站如何seo推广网站代运营多少钱一个月
  • 网站模板怎么改百度人气榜排名
  • 双浩建设网站宜家家居官网网上商城
  • 哪个网站做加盟网站建设技术李京文
  • ih5做自适应网站四川建设考试培训网
  • 制作网站的成本自建站电商外贸
  • 网站建设要考虑的问题哪建设网站
  • 网站建设状况专业网站优化软件
  • 网站是公司域名是个人可以宁波企业自助建站