1、ReadWriteLock
package com.JUC编程.代码;
import java.util.HashMap;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ReadWriteLockDemo {
public static void main(String[] args) {
MyReadWriteLock myReadWriteLock = new MyReadWriteLock();
//读
for (int i = 1; i <=5; i++) {
final int temp=i;
new Thread(()->{
myReadWriteLock.Write(temp+"",temp+"");
},String.valueOf(i)).start();
}
//写
for (int i = 1; i <=5; i++) {
final int temp=i;
new Thread(()->{
myReadWriteLock.Read(temp+"");
},String.valueOf(i)).start();
}
}
}
class MyReadWriteLock{
private volatile HashMap<String,String> hashMap=new HashMap<>();
ReadWriteLock lock= new ReentrantReadWriteLock();
public void Write(String s1,String s2){
lock.writeLock().lock();
try {
System.out.println(Thread.currentThread().getName()+"开始写入");
hashMap.put(s1,s2);
System.out.println(Thread.currentThread().getName()+"写入完成");
} catch (Exception e) {
e.printStackTrace();
}finally {
lock.writeLock().unlock();
}
}
public void Read(String key){
lock.readLock().lock();
try {
System.out.println(Thread.currentThread().getName()+"开始读");
String s = hashMap.get(key);
System.out.println(Thread.currentThread().getName()+"读完成");
} catch (Exception e) {
e.printStackTrace();
}finally{
lock.readLock().unlock();
}
}
}
2、BlockingQueue
(1)队列
队列的特点:先进后出,对头出,对尾入。
什么时候会发生阻塞?
- 对列已满,但还想再入队列
- 队列为空,想要出队列
(2)阻塞队列的继承关系
(3)使用
四组API
方法 | 抛出异常 | 不抛出异常 | 阻塞等待 | 超时等待 |
---|---|---|---|---|
添加 | add | offer | put | offer (参数可以设置阻塞时间) |
移除 | remove | poll | take | poll(参数可以设置阻塞时间) |
/*
* 抛出异常
* */
public static void test(){
ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);
System.out.println(arrayBlockingQueue.add("a"));
System.out.println(arrayBlockingQueue.add("b"));
System.out.println(arrayBlockingQueue.add("c"));
//抛出异常Exception in thread "main" java.lang.IllegalStateException: Queue full
//arrayBlockingQueue.add("d");
System.out.println("=======================");
System.out.println(arrayBlockingQueue.remove());
System.out.println(arrayBlockingQueue.remove());
System.out.println(arrayBlockingQueue.remove());
//抛出异常Exception in thread "main" java.util.NoSuchElementException
//System.out.println(arrayBlockingQueue.remove());
}
public static void test1(){
/*
* 不抛出异常
* */
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.offer("d"));//false
System.out.println(arrayBlockingQueue.poll());
System.out.println(arrayBlockingQueue.poll());
System.out.println(arrayBlockingQueue.poll());
System.out.println(arrayBlockingQueue.poll());//null
}
public static void test2() throws InterruptedException {
/*
* 一直阻塞等待
* */
ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);
arrayBlockingQueue.put("a");
arrayBlockingQueue.put("b");
arrayBlockingQueue.put("c");
//如果队列满 就会一直阻塞
//arrayBlockingQueue.put("d");
System.out.println(arrayBlockingQueue.take());
System.out.println(arrayBlockingQueue.take());
System.out.println(arrayBlockingQueue.take());
//队列为空 一直阻塞
//System.out.println(arrayBlockingQueue.take());
}
public static void test3() throws InterruptedException {
/*
*阻塞等待 超时退出
* */
ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);
arrayBlockingQueue.offer("a");
arrayBlockingQueue.offer("b");
arrayBlockingQueue.offer("c");
// arrayBlockingQueue.offer("d",2, TimeUnit.SECONDS);
System.out.println(arrayBlockingQueue.poll());
System.out.println(arrayBlockingQueue.poll());
System.out.println(arrayBlockingQueue.poll());
System.out.println(arrayBlockingQueue.poll(2,TimeUnit.SECONDS));
}
3、SynchronousQueue
package com.JUC编程.代码;
import java.util.concurrent.SynchronousQueue;
public class SynchronousQueueDemo {
public static void main(String[] args) {
//SynchronousQueue同步队列可以说没有容量,put了一个元素,必须先take出来
SynchronousQueue<String> queue = new SynchronousQueue<>();
new Thread(()->{
try {
System.out.println(Thread.currentThread().getName()+" put a");
queue.put("a");
System.out.println(Thread.currentThread().getName()+" put b");
queue.put("b");
System.out.println(Thread.currentThread().getName()+" put c");
queue.put("c");
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
System.out.println("take "+queue.take());
System.out.println("take "+queue.take());
System.out.println("take "+queue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
转载:https://blog.csdn.net/weixin_44324174/article/details/105486417
查看评论