(一)并发,异步基本概念
并发:可以理解为多线程操作
异步:主线程中添加了子线程,子线程的成成功与否,运行到什么阶段不影响主线程。
(二)异步,并发的代码实现
并发
//package com.gildata.task.platform.provider.timer.temp; // //import org.junit.jupiter.api.Assertions; //import org.junit.jupiter.api.Test; // //import java.util.concurrent.CompletableFuture; //import java.util.concurrent.ExecutionException; //import java.util.concurrent.ExecutorService; //import java.util.concurrent.Executors; ///** // * @author cuitao // * @ className: // * @ description: // * @ create 2020-12-22 16:14 // **/ // ///** // * 并行获取各个数据源的数据合并成一个数据组合 // */ //public class ParallelTest { // ExecutorService executor = Executors.newFixedThreadPool(100); // /** // * 获取基本信息 // * // * @return // */ // public String getProductBaseInfo(String productId) throws InterruptedException { // Thread.sleep(2000); // return productId + "商品基础信息"; // } // // /** // * 获取详情信息 // * // * @return // */ // public String getProductDetailInfo(String productId) throws InterruptedException { // Thread.sleep(2000); // return productId + "商品详情信息"; // } // // /** // * 获取sku信息 // * // * @return // */ // public String getProductSkuInfo(String productId) throws InterruptedException { // Thread.sleep(2000); // return productId + "商品sku信息"; // } // // /** // * 取得一个商品的所有信息(基础、详情、sku) // * // * @param productId // * @return // */ // public String getAllInfoByProductId(String productId) { // CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> getProductBaseInfo(productId),executor); // CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> getProductDetailInfo(productId),executor); // CompletableFuture<String> f3 = CompletableFuture.supplyAsync(() -> getProductSkuInfo(productId),executor); // // try { // String baseInfo = f1.get(); // String detailInfo = f2.get(); // String skuInfo = f3.get(); // return baseInfo + "" + detailInfo + skuInfo; // } catch (InterruptedException e) { // e.printStackTrace(); // } catch (ExecutionException e) { // e.printStackTrace(); // } // return null; // } public String getAllInfoByProductId(String productId) { CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> { try { return getProductBaseInfo(productId); } catch (InterruptedException e) { e.printStackTrace(); } return null; }); CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> { try { return getProductDetailInfo(productId); } catch (InterruptedException e) { e.printStackTrace(); } return null; }); CompletableFuture<String> f3 = CompletableFuture.supplyAsync(() -> { try { return getProductSkuInfo(productId); } catch (InterruptedException e) { e.printStackTrace(); } return null; }); //等待三个数据源都返回后,再组装数据。这里会有一个线程阻塞 CompletableFuture.allOf(f1, f2, f3).join(); try { String baseInfo = f1.get(); String detailInfo = f2.get(); String skuInfo = f3.get(); return baseInfo + "" + detailInfo + skuInfo; } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } return null; } // // /** // * 并行获取数据的计算 // */ // @Test // public void testGetAllInfoParalleByProductId() throws ExecutionException, InterruptedException { // ParallelTest test = new ParallelTest(); // long startTime = System.currentTimeMillis(); // String info = test.getAllInfoByProductId("1111"); // long endTime = System.currentTimeMillis(); // System.out.println("并发用时:" + (endTime - startTime)); // Assertions.assertNotNull(info); // } // // /** // * 同步获取执行的处理 // */ // @Test // public void testGetAllInfoDirectly() throws ExecutionException, InterruptedException { // ParallelTest test = new ParallelTest(); // long startTime = System.currentTimeMillis(); // String info1 = getProductBaseInfo("1111"); // String info2 = getProductDetailInfo("1111"); // String info3 = getProductSkuInfo("1111"); // long endTime = System.currentTimeMillis(); // System.out.println("串行用时:" + (endTime - startTime)); // String info = info1 + "" + info2 + info3; // Assertions.assertNotNull(info); // // } //}
异步
启动类添加注解:@EnableAsync @EnableAsync public class ServerStart { public static void main(String[] args) { SpringApplication.run(ServerStart.class); } } service 添加注解:@Async @Service @Async public class TaskAsyncServiceImpl implements TaskAsyncService {