feat: 增加ttl 使用实例

This commit is contained in:
wyl
2023-06-25 22:47:00 +08:00
parent 73d5c27005
commit 50487c863a
36 changed files with 1203 additions and 135 deletions
@@ -0,0 +1,127 @@
package concurrent.completableFuture;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@Slf4j
public class completableFutureTest {
/**
* 有返回值的异步任务
*/
@Test
public void supplyAsyncGet() throws ExecutionException, InterruptedException {
CompletableFuture<String> supplyAsyncFuture = CompletableFuture.supplyAsync(() -> {
log.info("executing supplyAsync task ...");
return "this is supplyAsync";
});
String s = supplyAsyncFuture.get();
System.out.println(s);
}
@Test
public void supplyAsyncGetTimeOut() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<String> supplyAsyncFuture = CompletableFuture.supplyAsync(() -> {
log.info("executing supplyAsync task ...");
return "this is supplyAsync";
});
String s = supplyAsyncFuture.get(10L, TimeUnit.MILLISECONDS);
System.out.println(s);
}
@Test
public void supplyAsyncJoin() {
CompletableFuture<String> supplyAsyncFuture = CompletableFuture.supplyAsync(() -> {
log.info("executing supplyAsync task ...");
return "this is supplyAsync";
});
String s = supplyAsyncFuture.join();
System.out.println(s);
}
/**
* 没有返回值的异步任务
*/
public void runAsyncGet() throws ExecutionException, InterruptedException {
CompletableFuture<Void> runAsyncFuture = CompletableFuture.runAsync(() -> {
log.info("executing runAsync task ...");
});
runAsyncFuture.get();
}
public void runAsyncGetTimeOut() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<Void> runAsyncFuture = CompletableFuture.runAsync(() -> {
log.info("executing runAsync task ...");
});
runAsyncFuture.get(10L, TimeUnit.MILLISECONDS);
}
public void runAsyncJoin() {
CompletableFuture<Void> runAsyncFuture = CompletableFuture.runAsync(() -> {
log.info("executing runAsync task ...");
});
runAsyncFuture.join();
}
/**
* 多任务并行 等待都完成
*/
@Test
public void allOf() throws ExecutionException, InterruptedException {
CompletableFuture<String> cf11 = CompletableFuture.supplyAsync(() -> {
log.info("executing supplyAsync task cf11 ...");
try {
TimeUnit.SECONDS.sleep(10L);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "this is supplyAsync";
});
CompletableFuture<String> cf12 = CompletableFuture.supplyAsync(() -> {
log.info("executing supplyAsync task cf12 ...");
return "this is supplyAsync1";
});
CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(cf11, cf12);
String s = cf12.get();
String s1 = cf11.get();
System.out.println(s);
System.out.println(s1);
// allOfFuture.get();
// String s = cf12.get();
// String s1 = cf11.get();
// System.out.println(s);
// System.out.println(s1);
}
/**
* 多任务并行 一个完成就返回完成的
*/
@Test
public void test() throws ExecutionException, InterruptedException {
CompletableFuture<String> cf21 = CompletableFuture.supplyAsync(() -> {
log.info("executing supplyAsync task cf21 ...");
try {
TimeUnit.SECONDS.sleep(1L);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "this is supplyAsync cf21";
});
CompletableFuture<String> cf22 = CompletableFuture.supplyAsync(() -> {
log.info("executing supplyAsync task cf22 ...");
return "this is supplyAsync cf22";
});
CompletableFuture<Object> anyOfFuture = CompletableFuture.anyOf(cf21, cf22);
log.info("{}", anyOfFuture.get()); // 输出结果:this is supplyAsync cf21或cf22
String join = cf21.join();
String join1 = cf22.join();
System.out.println(join);
System.out.println(join1);
}
}
@@ -3,9 +3,6 @@ package concurrent.hashmap;
import org.junit.jupiter.api.Test;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
public class HashMapTest {
static final int MAXIMUM_CAPACITY = 1 << 30;
@@ -2,7 +2,10 @@ package concurrent.model;
import org.junit.jupiter.api.Test;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
@@ -11,10 +14,10 @@ public class ModelTest {
@Test
public void ArrayBlockingQueueModelTest() throws InterruptedException {
ArrayBlockingQueue<Integer> arrayBlockingQueue = new ArrayBlockingQueue<>(5000);
for (int i = 0; i < 1000; i++){
for (int i = 0; i < 1000; i++) {
new QueueProducer(arrayBlockingQueue, i).start();
}
for (int i = 0; i < 200000; i++){
for (int i = 0; i < 200000; i++) {
new QueueConsumer(arrayBlockingQueue, i).start();
}
@@ -24,10 +27,10 @@ public class ModelTest {
@Test
public void LinkedBlockingQueueModelTest() throws InterruptedException {
LinkedBlockingDeque<Integer> arrayBlockingQueue = new LinkedBlockingDeque<>(1000);
for (int i = 0; i < 1000; i++){
for (int i = 0; i < 1000; i++) {
new QueueProducer(arrayBlockingQueue, i).start();
}
for (int i = 0; i < 200000; i++){
for (int i = 0; i < 200000; i++) {
new QueueConsumer(arrayBlockingQueue, i).start();
}
@@ -39,12 +42,50 @@ public class ModelTest {
Object o = new Object();
Queue queue = new LinkedList();
Integer maxSize = 100;
for (int i = 0; i < 15; i++){
for (int i = 0; i < 15; i++) {
new WaitProducer(maxSize, o, queue).start();
}
for (int i = 0; i < 2; i++){
for (int i = 0; i < 2; i++) {
new WaitConsumer(o, queue).start();
}
Thread.sleep(10000000);
}
@Test
public void test() throws InterruptedException {
Map<WeakReference<Integer>, WeakReference<Integer>> map = new HashMap<>(8);
// 注意这里~
WeakReference<Integer> key = new WeakReference<>(1);
WeakReference<Integer> value = new WeakReference<>(2);
map.put(key, value);
System.out.println("put success");
Thread.sleep(1000);
System.gc();
System.out.println("get " + map.get(key).get());
}
@Test
public void test2() throws InterruptedException {
Map<WeakReference<Integer>, WeakReference<Integer>> map = new HashMap<>(8);
WeakReference<Integer> key = new WeakReference<>(1);
WeakReference<Integer> value = new WeakReference<>(777);
map.put(key,value);
System.out.println("put success");
Thread.sleep(1000);
System.gc();
System.out.println("get " + map.get(key).get());
}
@Test
public void test3() throws InterruptedException {
Map<WeakReference<Integer>, Integer> map = new HashMap<>(8);
WeakReference<Integer> key = new WeakReference<>(1);
Integer value = 777;
map.put(key,value);
System.out.println("put success");
Thread.sleep(1000);
System.gc();
System.out.println("get " + map.get(key));
}
}