1、读写锁ReadWriteLock

package com.readlock;

import java.util.HashMap;
import java.util.Map; /**
* ReadWriteLock
*/
public class ReadWriteLockDemo {
public static void main(String[] args) {
MyCache myCache = new MyCache(); for (int i = 0; i < 5; i++) {
final int temp = i;
new Thread(()->{
myCache.put(temp+"",temp+"");//写入
},String.valueOf(i)).start(); } for (int i = 0; i < 5; i++) {
final int temp = i;
new Thread(()->{
myCache.get(temp+"");//读取
},String.valueOf(i)).start(); } } } class MyCache{
private volatile Map<String,Object> map =new HashMap<>(); //写
public void put(String key,Object value){
System.out.println(Thread.currentThread().getName()+"写入"+key);
map.put(key,value);
System.out.println(Thread.currentThread().getName()+"写入成功"); } //读
public void get(String key){
System.out.println(Thread.currentThread().getName()+"读取"+key);
Object o = map.get(key);
System.out.println(Thread.currentThread().getName()+"读取成功"); }
}

测试结果(正常情况下:写入、写入成功)

2、阻塞队列

  • 写入:如果队列满了,就必须阻塞等待
  • 取:如果是队列是空的,必须阻塞等待产生


BlockingQueue
什么情况下我们会使用阻塞队列:多线程并发处理,线程池!

学会使用队列
添加、移除

3、四组API

1、抛出异常

  • 2、不会抛出异常
  • 3、阻塞、等待
  • 4、超时等待
方式 抛出异常 有返回值,不抛出异常 阻塞等待 超时等待
添加 add offer() put() offer(,)
移除 remove poll() take() poll(,)
判断队列首 element peek - -

2.1 抛出异常

add 和remove会抛出异常。队列满继续添加数据到队列,队列空,继续取出元素

package com.blockingqueue;

import java.util.concurrent.ArrayBlockingQueue;

public class BlockingQueueDemo {
public static void main(String[] args) {
test1();
} /**
* 抛出异常
*/
public static void test1(){
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
System.out.println(blockingQueue.add("a"));
System.out.println(blockingQueue.add("b"));
System.out.println(blockingQueue.add("c")); /**
* 抛出异常:java.lang.IllegalStateException: Queue full
* System.out.println(blockingQueue.add("c"));
*/
System.out.println("===========================");
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove()); /**
*取出所有的元素之后、再次取出元素
* java.util.NoSuchElementException
* System.out.println(blockingQueue.remove());
*/ }
}

查看队首元素

2.2 不抛出异常

off 和 poll

使用offer不抛出异常

使用poll、源码

2.3 阻塞等待

put 和 take

2.4 超时等待

如果队列满了、设置超时时间、当过了这个超时时间,自动退出

最新文章

  1. [LeetCode] Path Sum III 二叉树的路径和之三
  2. 图片采用base64压缩,可以以字符串的形式传送base64给服务端转存为图片
  3. Click Magick – 下一代点击跟踪和链接管理
  4. MVC5 + EF6 + Bootstrap3 (15) 应用ModelState和Data Annotation做服务器端数据验证
  5. Emacs+highlight-parentheses高亮括号
  6. 图像金字塔及其在 OpenCV 中的应用范例(上)
  7. 使用Uploadify 时,同时使用了jQuery.Validition 验证控件时,在IE11上出现JS缺少对象错误。
  8. CSS网页元素居中
  9. Swift - 移除页面视图上的所有元素
  10. Pygame制作微信打飞机游戏PC版
  11. 为知笔记markdown插件安装
  12. pandas小记:pandas基本设置
  13. python摸爬滚打之day030----进程
  14. 搭建SpringBoot+dubbo+zookeeper+maven框架(四)
  15. 【BZOJ4890】[TJOI2017]城市(动态规划)
  16. es6写法
  17. nginx安装升级及配置详解
  18. kettle实现数据库迁移----多表复制向导
  19. November 6th 2016 Week 46th Sunday
  20. 循环赛日常表算法(N可为奇数和偶数)

热门文章

  1. 字符编码和python中的文件处理
  2. ceph 008 ceph多区域网关(ceph对象容灾) cephfs文件系统
  3. HCIA-Datacom 2.2 实验:OSPF路由协议基础实验
  4. Web 布局设计(一):固定侧边栏
  5. STC8H开发(十六): GPIO驱动XL2400无线模块
  6. Python自学教程2:大牛们怎么写注释
  7. web监听器解析
  8. QPainter. QpaintDevice 绘图设备
  9. Windows平台RTMP/RTSP播放器实现实时音量调节
  10. 第十章 Kubernetes的CNI网络插件--flannel