目录
Semaphore 实现快速限流
读写锁ReentrantReadWriteLock实现缓存
CountDownLatch的使用
futureTask的使用
CompletableFuture的使用
CompletionService
Fork/Join 的使用
Lock
支持超时,支持非阻塞获取锁,以及支持响应中断
Semaphore 实现快速限流
private final Semaphore semaphore;
private final List pool;
public SemaphorePool(int poolSize, T t) {
pool = new Vector() {
};
for (int i = 0; i < poolSize; i++) {
pool.add(t);
}
semaphore = new Semaphore(poolSize);
}
public void exec(Function func) {
T t = null;
try {
//当semaphore的值<0,线程会被阻塞
semaphore.acquire();
t = pool.remove(0);
func.apply(t);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
pool.add(t);
semaphore.release();
}
}
public static void main(String[] args) {
SemaphorePool pool =
new SemaphorePool<>(10, (long) 10);
pool.exec(t -> {
System.out.println(t);
return t.toString();
});
}
读写锁ReentrantReadWriteLock实现缓存
public class Cache {
public Map catchMap = new HashMap<>();
public final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
public final Lock readLock = readWriteLock.readLock();
public final Lock writeLock = readWriteLock.writeLock();
public V get(K key) {
V value = null;
readLock.lock();
try {
value = catchMap.get(key);
} finally {
readLock.unlock();
}
if (value != null) {
return value;
}
//缓存不存在,查询数据库,高并发场景下,多个线程同时执行到这
writeLock.lock();
try {
//再次验证 已经有线程查询过数据库,并更新了数据,则直接获取数据
value = catchMap.get(key);
if (value == null) {
//查询数据库的数据
value = null;
catchMap.put(key, value);
}
} finally {
writeLock.unlock();
}
return value;
}
public void put(K key, V value) {
writeLock.lock();
try {
catchMap.put(key, value);
} finally {
writeLock.unlock();
}
}
}
CountDownLatch的使用
public class Cache{ public Map catchMap = new HashMap<>(); public final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); public final Lock readLock = readWriteLock.readLock(); public final Lock writeLock = readWriteLock.writeLock(); public V get(K key) { V value = null; readLock.lock(); try { value = catchMap.get(key); } finally { readLock.unlock(); } if (value != null) { return value; } //缓存不存在,查询数据库,高并发场景下,多个线程同时执行到这 writeLock.lock(); try { //再次验证 已经有线程查询过数据库,并更新了数据,则直接获取数据 value = catchMap.get(key); if (value == null) { //查询数据库的数据 value = null; catchMap.put(key, value); } } finally { writeLock.unlock(); } return value; } public void put(K key, V value) { writeLock.lock(); try { catchMap.put(key, value); } finally { writeLock.unlock(); } } }
CountDownLatch的使用
CountDownLatch 主要用来解决一个线程等待多个线程的场景
public ExecutorService executorService = Executors.newFixedThreadPool(2);
//主方法
public void executor() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(2);
executorService.submit(new Runnable() {
//T1
@Override
public void run() {
test1();
countDownLatch.countDown();
}
});
executorService.submit(new Runnable() {
//T2
@Override
public void run() {
test2();
countDownLatch.countDown();
}
});
//利用countdonwLatch实现当T1与T2执行完,才能执行接下来的操作,await当值>0会一直阻塞
countDownLatch.await();
System.out.println("两者都执行完成");
}
public void test1() {
System.out.println("执行完test1");
}
public void test2() {
System.out.println("执行完test2");
}
futureTask的使用
实现以下代码
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(2);
FutureTask futureTask2 = new FutureTask<>(new Task2());
FutureTask futureTask1 = new FutureTask<>(new Task1(futureTask2));
executorService.submit(futureTask1);
executorService.submit(futureTask2);
}
public static class Task1 implements Callable {
private FutureTask task2;
public Task1(FutureTask task2) {
this.task2 = task2;
}
@Override
public String call() throws Exception {
System.out.println("Task1: 洗水壶...");
TimeUnit.SECONDS.sleep(1);
System.out.println("Task1: 烧开水...");
TimeUnit.SECONDS.sleep(15);
//当前的代码是阻塞作用,只有当线程task2执行完。才可能继续执行,
//否则线程会一直阻塞
Object tf = task2.get();
System.out.println("Task1: 拿到茶叶:" + tf);
System.out.println("Task1: 泡茶...");
return " 上茶:" + tf;
}
}
public static class Task2 implements Callable {
@Override
public Object call() throws Exception {
System.out.println("T2: 洗茶壶...");
TimeUnit.SECONDS.sleep(1);
System.out.println("T2: 洗茶杯...");
TimeUnit.SECONDS.sleep(2);
System.out.println("T2: 拿茶叶...");
TimeUnit.SECONDS.sleep(1);
return " 龙井 ";
}
}
利用completableFuture实现上述流程
CompletableFuturef1 = CompletableFuture.runAsync(() -> { try { System.out.println("T1: 洗水壶..."); TimeUnit.SECONDS.sleep(1); System.out.println("T1: 烧开水..."); TimeUnit.SECONDS.sleep(15); } catch (Exception e) { } }); CompletableFuture f2 = CompletableFuture.supplyAsync(() -> { try { System.out.println("T2: 洗茶壶..."); TimeUnit.SECONDS.sleep(1); System.out.println("T2: 洗茶杯..."); TimeUnit.SECONDS.sleep(2); System.out.println("T2: 拿茶叶..."); TimeUnit.SECONDS.sleep(1); return " 龙井 "; } catch (Exception e) { } return null; }); CompletableFuture f3 = f1.thenCombine(f2, (__, s) -> { System.out.println("T3: 拿到茶叶:" + s); System.out.println("T3: 泡茶..."); return "喝茶"; });
CompletableFuture的使用
ExecutorService executorService = Executors.newFixedThreadPool(3);
//Runnable接口无返回值
public static class Task1 implements Runnable {
@Override
public void run() {
System.out.println("hello");
}
}
//有返回值
public static class Task2 implements Callable {
@Override
public Object call() throws Exception {
return "hello word";
}
}
//执行无返回值的异步操作
public void runAsyncTest() {
//runAsync(Runnable runnable)使用公共的 ForkJoinPool 线程池,
// 这个线程池默认创建的线程数是 CPU 的核数
// (也可以通过 JVM option:-Djava.util.concurrent.ForkJoinPool.common.parallelism
// 来设置 ForkJoinPool 线程池的线程数 (不推荐,如果线程存在阻塞,会导致整个业务瘫痪,没有足够的ForkJoinPool运行任务)
//runAsync(Runnable runnable, Executor executor) //使用自己定义的线程池,推荐使用
CompletableFuture.runAsync(new Task1());
CompletableFuture.runAsync(new Task1(), executorService);
}
//执行有返回值的异步操作
public void supplyAsyncTest() throws ExecutionException, InterruptedException {
//supplyAsync(Supplier supplier) 不能传递参数,并返回对应类型结果
CompletableFuture stringFuture = CompletableFuture.supplyAsync(() -> "hello");
CompletableFuture.supplyAsync(() -> "hello", executorService);
}
public void CompletionStageTest() {
CompletableFuture f1 = CompletableFuture.runAsync(() -> System.out.println(1));
//thenApply(func) 有传参,有返回值,__两个下划线代表传入参数为空(void的含义)
f1.thenApply((__) -> "ss");
//thenAccept(comsumer) 有入参,无返回值
f1.thenAccept((s) -> System.out.println(s));
//thenRun(Runnable 无入参和返回值)
f1.thenRun(() -> System.out.println(1));
//thenCompose(Function super T, ? extends CompletionStage> fn) 内部会再创建一个CompletionStage子线程
f1.thenCompose((s) -> CompletableFuture.supplyAsync(() -> "1"));
//方法带有Async都是异步
}
public void CompletionStageAndTest() {
CompletableFuture f1 = CompletableFuture.runAsync(() -> System.out.println(1));
CompletableFuture f2 = CompletableFuture.runAsync(() -> System.out.println(1));
//thenCombine(CompletionStage extends U> other,BiFunction super T,? super U,? extends V> fn)
// 第一个参数为另一个CompletionStage,第二个参数BitFunction,有两个入参,有返回值 R apply(T t, U u);
f1.thenCombine(f2, (__, s) -> "ss");
//thenAcceptBoth(CompletionStage extends U> other,BiConsumer super T, ? super U> action)
//第一个参数为另一个CompletionStage,第二个参数BitConsume,有两个入参,无返回值 void accept(T t, U u);
f1.thenAcceptBoth(f2, (__, s) -> System.out.println("hello"));
// runAfterBoth(CompletionStage> other,Runnable action)
f1.runAfterBoth(f2, () -> System.out.println("hello runnable"));
}
public void CompletionStageEtherTest() {
CompletableFuture f1 = CompletableFuture.runAsync(() -> System.out.println(1));
CompletableFuture f2 = CompletableFuture.runAsync(() -> System.out.println(1));
//acceptEither(CompletionStage extends T> other, Consumer super T> action)
f1.acceptEither(f2, (__) -> System.out.println("hello"));
//applyToEither(other,fun)
f1.applyToEither(f2, (__) -> "hell");
//runAfterEither(other, runnable)
f1.runAfterEither(f2, () -> System.out.println(1));
}
public void exceptionTest() throws ExecutionException, InterruptedException {
CompletableFuture
f0 = CompletableFuture
.supplyAsync(() -> 7 / 0)
.thenApply(r -> r * 10)
.exceptionally(e -> {
System.out.println("error");
return 0;
});
//whenComplete与handle类型与try{}finally的用法 ,whenComplete无返回值
CompletableFuture
.runAsync(() -> System.out.println("hee"))
.whenComplete((followRes, e) -> System.out.println(e)); //第一个变量是上游的返回值,第二个变量为当前相传的变量,如果不想引用上游的值,可以用__表示空
CompletableFuture res = CompletableFuture.runAsync(() -> System.out.println("hee"))
.handle((__, e) -> {
return "ee";
});
System.out.println(res.get());
}
CompletionService
用于批量提交异步任务,CompletionService 能够让异步任务的执行结果有序化,先执行完的先进入阻塞队列
public void testCompletableService() throws InterruptedException, ExecutionException {
ExecutorService executor =
Executors.newFixedThreadPool(3);
// 创建 CompletionService
CompletionService cs = new
ExecutorCompletionService<>(executor);
// 异步查询结果
cs.submit(() -> 1);
// 异步查询结果
cs.submit(() -> 2);
// 异步查询结果
cs.submit(() -> 3);
// 将查询结果存储
for (int i = 0; i < 3; i++) {
//take和poll都是阻塞方法,如果阻塞队列为空
//take会一直阻塞
//poll会获取null值
Integer r = cs.take().get();
executor.execute(() -> System.out.println(r));
}
}
Fork/Join 的使用
Fork/Join 是一个并行计算的框架,主要就是用来支持分治任务模型的,这个计算框架里的Fork 对应的是分治任务模型里的任务分解,Join 对应的是结果合并,一部分是分治任务的线程池 ForkJoinPool,另一部分是分治任务 ForkJoinTask
ForkJoinTask 是一个抽象类,核心方法 fork() 和 join() ,其中 fork() 会异步地执行一个子任务,而 join() 会阻塞当前线程来等待子任务的执行结果,ForkJoinPool 支持任务窃取机制,能够让所有线程的工作量基本均衡,不会出现有的线程很忙,而有的线程很闲的状况
Fork/join实现大数据中的wordCount
public static void main(String[] args) {
String[] words = {"hello word", "java is good", "hello cs", "good idea"};
ForkJoinPool forkJoinPool = new ForkJoinPool(3);
Map invoke = forkJoinPool.invoke(new WordCountMR(words, 0, words.length));
System.out.println("hell");
}
//返回值为key,count键值对
public static class WordCountMR extends RecursiveTask



