zoukankan      html  css  js  c++  java
  • Spring Boot 中的异步调用

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.6.2</version>
    		<relativePath/>
    	</parent>
    	<groupId>com.hundsun</groupId>
    	<artifactId>springboot-async</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<name>springboot-async</name>
    	<properties>
    		<java.version>1.8</java.version>
    	</properties>
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    	</dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    			</plugin>
    		</plugins>
    	</build>
    
    </project>
    package com.hundsun.springbootasync.service;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MainApp {
    
        private Logger logger = LoggerFactory.getLogger(getClass());
    
        @Async
        public void asyncShow01() {
            sleep();
            logger.info("异步方法内部线程名称:{}", Thread.currentThread().getName());
        }
    
        public void syncShow02() {
            sleep();
            logger.info("同步方法内部线程名称:{}", Thread.currentThread().getName());
        }
    
        public void sleep() {
            long number = 0;
            for (long i = 0; i < 100000000; i++) {
                number+=i;
            }
            logger.info("总和:{}",number);
        }
    }
    package com.hundsun.springbootasync;
    
    import org.junit.jupiter.api.Test;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.scheduling.annotation.EnableAsync;
    
    import com.hundsun.springbootasync.service.MainApp;
    
    @EnableAsync // 开启异步支持
    @SpringBootTest
    class SpringbootAsyncApplicationTests {
    
        private Logger logger = LoggerFactory.getLogger(this.getClass());
    
        @Autowired
        private MainApp mainApp;
    
        @Test
        void test01() {
            long start = System.currentTimeMillis();
            mainApp.asyncShow01();
            long end = System.currentTimeMillis();
            logger.info("总耗时:{} ms", end - start);
        }
    
        @Test
        void test02() {
            long start = System.currentTimeMillis();
            mainApp.syncShow02();
            long end = System.currentTimeMillis();
            logger.info("总耗时:{} ms", end - start);
        }
    
    }
  • 相关阅读:
    WINCE6.0去掉桌面快捷方式
    dp和px,那些不得不吐槽的故事——Android平台图
    Redpine Signals RS9110-N-11-02 Wi-Fi解决方案
    Redpine的Lite-Fi解决方案获Wi-Fi CERTIFIED认证
    ARM处理器全解析:A8/A9/A15都是什么?
    Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) E
    Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) D
    Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C
    Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B
    Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) A
  • 原文地址:https://www.cnblogs.com/w1440199392/p/15771823.html
Copyright © 2011-2022 走看看