text:(连接池失效,异步编排实现)
parent
1359856950
commit
32a10733c9
|
@ -0,0 +1,55 @@
|
||||||
|
package rule.engine.domain;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassDescription:
|
||||||
|
* @JdkVersion: 17
|
||||||
|
* @Author: zhangxu
|
||||||
|
* @Created: 2024/5/12 20:27
|
||||||
|
*/
|
||||||
|
public class AsyncCompositionExample {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 创建线程池
|
||||||
|
ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||||
|
|
||||||
|
// 异步任务1:获取用户信息
|
||||||
|
CompletableFuture<String> getUserInfo = CompletableFuture.supplyAsync(() -> {
|
||||||
|
// 模拟耗时操作
|
||||||
|
try {
|
||||||
|
Thread.sleep(2000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return "User: John Doe";
|
||||||
|
}, executor);
|
||||||
|
|
||||||
|
// 异步任务2:获取用户订单信息
|
||||||
|
CompletableFuture<String> getUserOrders = CompletableFuture.supplyAsync(() -> {
|
||||||
|
// 模拟耗时操作
|
||||||
|
try {
|
||||||
|
Thread.sleep(3000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return "Orders: Order1, Order2";
|
||||||
|
}, executor);
|
||||||
|
|
||||||
|
// 异步任务组合:等待两个任务都完成后,将它们的结果进行处理
|
||||||
|
CompletableFuture<Void> combinedFuture = getUserInfo.thenCombine(getUserOrders, (userInfo, userOrders) -> {
|
||||||
|
// 对两个异步任务的结果进行处理
|
||||||
|
return "User Info: " + userInfo + ", " + userOrders;
|
||||||
|
}).thenAccept(result -> {
|
||||||
|
// 处理组合结果
|
||||||
|
System.out.println("Combined Result: " + result);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 等待异步任务组合完成
|
||||||
|
combinedFuture.join();
|
||||||
|
|
||||||
|
// 关闭线程池
|
||||||
|
executor.shutdown();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
package rule.engine.domain;
|
||||||
|
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassDescription:
|
||||||
|
* @JdkVersion: 17
|
||||||
|
* @Author: zhangxu
|
||||||
|
* @Created: 2024/5/12 20:04
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
public class AsyncExample {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||||
|
|
||||||
|
log.info("开始主程序");
|
||||||
|
|
||||||
|
//异步执行任务1
|
||||||
|
CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
|
||||||
|
log.info("开始执行任务1");
|
||||||
|
try {
|
||||||
|
//模拟耗时操作
|
||||||
|
Thread.sleep(2000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
log.info("任务一执行完毕");
|
||||||
|
});
|
||||||
|
|
||||||
|
//异步执行任务2
|
||||||
|
CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
|
||||||
|
log.info("开始执行任务2");
|
||||||
|
try {
|
||||||
|
//模拟耗时操作
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("任务2执行完毕");
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
//等待所有任务完成
|
||||||
|
CompletableFuture.allOf(future1,future2).get();
|
||||||
|
log.info("所有任务执行完毕");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package rule.engine.domain;
|
||||||
|
|
||||||
|
import com.zx.domain.DataSource;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassDescription:
|
||||||
|
* @JdkVersion: 17
|
||||||
|
* @Author: zhangxu
|
||||||
|
* @Created: 2024/5/12 9:54
|
||||||
|
*/
|
||||||
|
public class ConnectionFacory {
|
||||||
|
|
||||||
|
private static ConnectionFacory instance;
|
||||||
|
|
||||||
|
private static DataSource dbConn=new DataSource();
|
||||||
|
|
||||||
|
private static String fileName=null; //数据库的文件名,先为null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 需要知道连接池中的连接是否呗使用
|
||||||
|
* 如果用Map的数据类型去保存
|
||||||
|
* time为初始化俩接的毫秒值,没有使用过的话设置为0,
|
||||||
|
* 从这数据库取时,设置获取当前的时间毫秒值。
|
||||||
|
* 并把这个连接假如到另外一个连接池中
|
||||||
|
*
|
||||||
|
* **/
|
||||||
|
|
||||||
|
private static Map<Connection,Long> connectionPoll = new HashMap<Connection,Long>();
|
||||||
|
/**
|
||||||
|
* 设置最大时间为一个常量
|
||||||
|
* */
|
||||||
|
private static long MAX_UNUSED_TIME=7*60*60*100;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用的连接都假如到这个链接池中
|
||||||
|
* */
|
||||||
|
private static Map<Connection,Long> connectionUserPool =new HashMap<Connection,Long>();
|
||||||
|
|
||||||
|
private ConnectionFacory(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue