首页 技术 正文
技术 2022年11月15日
0 收藏 523 点赞 4,744 浏览 1983 个字

  线程中的知识点基本都已经学完了,看看Java5并发库中提供的集合。。。

一、可堵塞队列

队列包含固定长度的队列和不固定长度的队列

ArrayBlockQueue中只有put()方法和take()方法才具有阻塞功能

1、阻塞队列的功能和效果,代码如下:

 import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue; /**
* @className: BlockingQueueTest
* @description: 可阻塞队列的应用
* @author: ssc
* @date: 2019年6月22日 上午11:07:22
*/
public class BlockingQueueTest { public static void main(String[] args) {
BlockingQueue queue = new ArrayBlockingQueue(3);
for (int i = 0; i < 2; i++) {
new Thread() {
@Override
public void run() {
while (true) {
try {
Thread.sleep((long) (Math.random() * 10000));
System.out.println(Thread.currentThread().getName() + "准备放数据");
// 往对列中放数据
queue.put(1);
System.out.println(Thread.currentThread().getName() + "已经放了数据,队列目前有" + queue.size() + "个数据");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
} new Thread() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10000);
System.out.println(Thread.currentThread().getName() + "准备取数据");
// 从队列中取出数据
queue.take();
System.out.println(Thread.currentThread().getName() + "已经取走数据,队列目前有" + queue.size() + "个数据");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start(); } }

2、堵塞队列来实现通知的功能

代码示例如下:

 import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue; public class Business { private BlockingQueue<Integer> queue1 = new ArrayBlockingQueue<Integer>(1);
private BlockingQueue<Integer> queue2 = new ArrayBlockingQueue<Integer>(1); // 这种写法是匿名构造方法 它在构造方法中优先级是最高的,所有构造方法之前首先执行
{
try {
queue2.put(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void sub(int i) { try {
// 队列1 要放入值
queue1.put(1);
for (int j = 1; j <= 10; j++) {
System.out.println("sub thread sequece of " + j + ", loop of " + i);
}
// 队列2 要把值取出来
queue2.take(); } catch (InterruptedException e) {
e.printStackTrace();
}
} public void main(int i) {
try {
queue2.put(1);
for (int j = 1; j <= 100; j++) {
System.out.println("main thread sequece of " + j + ", loop of " + i);
}
queue1.take();
} catch (InterruptedException e) {
e.printStackTrace();
} }
}

二、同步集合(并发集合)类

传统集合在并发访问时是有问题的

Java5中提供了一些同步集合类:

ConcurrentMap

CopyOnWriteArrayList

CopyOnWriteArraySet

就是这些集合类是线程安全的,即使在多线程的环境下,也不会存在并发问题,用法是和基本的集合类是一样的,只不过JDK中实现了线程同步的代码!!!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291