抛出异常 |
返回值 |
阻塞 |
定时阻塞 |
|
添加 |
add |
offer - true/false |
put |
offer |
获取 |
remove |
poll - null |
take |
po |
package cn.tedu.concurrent;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
public class BlockingQueueDemo {
public static void main(String[] args) throws InterruptedException {
// 创建一个阻塞式队列
ArrayBlockingQueue queue = new ArrayBlockingQueue<>(5);
// 存放元素
// queue.add("a");
// queue.add("b");
// queue.add("c");
// queue.add("d");
// queue.add("e");
// 获取元素
// 队列为空
// 抛出异常
// System.out.println(queue.remove());
// 返回null
// System.out.println(queue.poll());
// 产生阻塞
// System.out.println(queue.take());
// 定时阻塞
System.out.println(queue.poll(5, TimeUnit.SECONDS));
// 队列已满
// 抛出异常
// queue.add("f");
// 返回false
// boolean b = queue.offer("f");
// System.out.println(b);
// 产生阻塞
// queue.put("f");
// 定时阻塞
// queue.offer("f", 5, TimeUnit.SECONDS);
// System.out.println(queue);
}
}
package cn.tedu.concurrent;
import java.util.concurrent.PriorityBlockingQueue;
//具有优先级的阻塞式队列
public class PriorityBlockingQueueDemo {
public static void main(String[] args) throws InterruptedException {
/* PriorityBlockingQueue queue = new PriorityBlockingQueue<>(5);
queue.put("a");
queue.put("h");
queue.put("d");
queue.put("w");
queue.put("x");
queue.put("z")*/
;
/*
1.在使用的时候可以指定容量也可以不指定。如果不指定容量,默认容量是11
2. 在拿取元素的时候,会对元素进行排序(自然排序 - 自然排序如果不指定一般是升序的) - 要求元素所对应的类必须实现Comparable接口,重写compareTo方法指定比较规则
*/
/* for(int i=0;i<6;i++){
System.out.println(queue.take());
}*/
PriorityBlockingQueue queue = new PriorityBlockingQueue<>();
queue.put(new Student("wyf", 40, 70));
queue.put(new Student("wyh", 25, 80));
queue.put(new Student("wyb", 30, 10));
queue.put(new Student("why", 60, 59));
queue.put(new Student("wyl", 50, 60));
queue.put(new Student("wyx", 11, 11));
for (int i = 0; i < 7; i++) {
System.out.println(queue.take());
}
/* for (Student s : queue) {
System.out.println(s);
}*/
}
}
class Student{
private String name;
private int age;
private int score;
public Student(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
public int compareTo(Student o) {
return o.score - this.score;
}
}
要求元素必须有序 |
针对原来的数据进行提取形成跳跃表,跳跃表可以向上一层层提取,但是最顶层的跳跃表中的元素至少有2个,一般是2-4个 |
跳跃表是典型的以空间换时间的产物 |
适用于查询多而增删少 |
如果新添节点,这个节点是否要提取到上一层跳跃表中,遵循"抛硬币"原则 |
跳跃7表的查询时间复杂度是O(logn) |
package cn.tedu.concurrent;
import java.util.concurrent.ConcurrentSkipListMap;
public class ConcurrentMapDemo {
//并发哈希映射
public static void main(String[] args) {
ConcurrentSkipListMap map = new ConcurrentSkipListMap<>();
map.put("a", 3);
map.put("d", 3);
map.put("e", 3);
map.put("w", 3);
map.put("q", 3);
map.put("m", 3);
map.put("e", 3);
//可以截取map表
System.out.println(map);
System.out.println(map.subMap("a", "w"));
System.out.println(map.headMap("e"));
System.out.println(map.tailMap("q"));
}
}
package cn.tedu.concurrent;
import java.util.concurrent.*;
//执行器服务
public class ExcutorServiceDemo {
public static void main(String[] args) {
//创建线程池
//corePoolSize 核心线程数量
//maximumPoolSize 线程池的大小=核心线程数+临时线程数
//keepAliveTime -临时线程的存活时间
//unit -时间单位
//workQueue - 工作队列
//handle -拒绝执行处理器
ExecutorService es = new ThreadPoolExecutor(5, 10, 5, TimeUnit.SECONDS,
new ArrayBlockingQueue(5), //工作队列
new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.out.println("拒绝执行" + r);
}
});
//提交线程去执行
for (int i = 0; i < 18; i++) {
es.execute(new EsRunable());
}
es.shutdown();
}
}
class EsRunable implements Runnable{
@Override
public void run() {
System.out.println("hello wyf");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package cn.tedu.concurrent;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ExecutorServiceDemo2 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
/*
特点:
1. 没有核心线程全部都是临时线程
2. 临时线程的数量是Integer.MAX_VALUE
人为的认为这个线程池中包含的线程数量是无限的
3. 临时线程用完之后允许存活1min
4. 工作队列是一个同步队列
5. 大池子小队列
适用场景:高并发、短任务场景
*/
// ExecutorService es = Executors.CachedThreadPool();
/*
特点:
1. 没有临时线程全部都是核心线程
2. 工作队列是一个阻塞式链式队列,默认容量是Integer.MAX_VALUE
可以认为能够存储无限多的任务
小池子大队列
适用场景:长任务
*/
ExecutorService es = Executors.newFixedThreadPool(5);
// 将结果封装成Future对象,泛型表示的结果的实际类型
Future f = es.submit(new CThread());
es.shutdown();
System.out.println(f.get());
}
}
// 泛型表示返回值的类型
class CThread implements Callable {
@Override
public String call() throws Exception {
return "SUCCESS";
}
}
package cn.tedu.concurrent;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;
//分叉合并池
public class ForkJoinDemo {
/* public static void main(String[] args) {
long begin = System.currentTimeMillis();
long sum = 0;
for (long i = 1; i <= 100000000000L; i++) {
sum += i;
}
//计算结果:932356074711512064
System.out.println(sum);
}*/
public static void main(String[] args) throws ExecutionException, InterruptedException {
long begin = System.currentTimeMillis();
ForkJoinPool pool = new ForkJoinPool();
ForkJoinTask f = pool.submit(new Sum(1, 00000000000L));
pool.shutdown();
System.out.println(f.get());
long end = System.currentTimeMillis();
System.out.println(end-begin);
}
}
class Sum extends RecursiveTask {
private static final long serialVersionUID = -5007766220903293812L;
private long start;
private long end;
public Sum(long start, long end) {
this.start = start;
this.end = end;
}
@Override
protected Long compute() {
if (end - start <= 10000) {
long sum = 0;
for (long i = start; i <= end; i++) {
sum += i;
}
return sum;
} else {
long mid = (start + end) / 2;
Sum left = new Sum(start, mid);
Sum right = new Sum(mid + 1, end);
// 分叉
left.fork();
right.fork();
// 合并
return left.join() + right.join();
}
}
}