SpringBoot實現異步多線程

一、啓動類中添加@EnableAsynchtml

@SpringBootApplication
@EnableAsync
public class ModelApplication {
    public static void main(String[] args) {
        SpringApplication.run(ModelApplication.class,args);
    }
}

二、新建異步線程類,並加上相應註解java

package com.ndsc.model.asybc;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class AsyncThread {
    @Async
    public void run(){
        System.out.println("異步線程:"+Thread.currentThread().getName());
    }
}

三、調用(以農村不起眼的商機controller層調用爲例)react

package com.ndsc.model.controller;

import com.ndsc.model.asybc.AsyncThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MyController {
    @Autowired
    AsyncThread asyncThread;

    @RequestMapping("/testAsync")
    public String testAsync(){
        System.out.println("當前線程:"+Thread.currentThread().getName());
        asyncThread.run();
        return "test";
    }
}

四、啓動測試linux

http://localhost:8099/testAsyncweb

"C:\Program Files\Java\jdk1.8.0_131\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=61554 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:C:\MyselfApplication\WinSoft\IDEA\install\IntelliJ IDEA 2018.3.2\lib\idea_rt.jar=61555:C:\MyselfApplication\WinSoft\IDEA\install\IntelliJ IDEA 2018.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\out\production\offLineSpringBoot;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\attoparser-2.0.5.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\classmate-1.4.0.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\HdrHistogram-2.1.9.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\hibernate-validator-6.0.17.Final.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\jackson-annotations-2.9.0.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\jackson-core-2.9.9.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\jackson-databind-2.9.9.3.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\jackson-datatype-jdk8-2.9.9.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\jackson-datatype-jsr310-2.9.9.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\jackson-module-parameter-names-2.9.9.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\javax.annotation-api-1.3.2.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\javax.el-3.0.0.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\jboss-logging-3.3.3.Final.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\jul-to-slf4j-1.7.28.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\LatencyUtils-2.0.3.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\log4j-api-2.11.2.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\log4j-to-slf4j-2.11.2.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\logback-classic-1.2.3.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\logback-core-1.2.3.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\micrometer-core-1.1.6.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\netty-buffer-4.1.39.Final.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\netty-codec-4.1.39.Final.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\netty-codec-http2-4.1.39.Final.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\netty-codec-http-4.1.39.Final.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\netty-codec-socks-4.1.39.Final.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\netty-common-4.1.39.Final.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\netty-handler-4.1.39.Final.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\netty-handler-proxy-4.1.39.Final.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\netty-resolver-4.1.39.Final.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\netty-transport-4.1.39.Final.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\netty-transport-native-epoll-4.1.39.Final-linux-x86_64.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\netty-transport-native-unix-common-4.1.39.Final.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\nio-multipart-parser-1.1.0.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\nio-stream-storage-1.1.3.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\reactive-streams-1.0.3.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\reactor-core-3.2.12.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\reactor-extra-3.2.3.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\reactor-netty-0.8.11.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\slf4j-api-1.7.28.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\snakeyaml-1.23.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\spring-aop-5.1.9.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\spring-beans-5.1.9.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\spring-boot-2.1.8.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\spring-boot-actuator-2.1.8.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\spring-boot-actuator-autoconfigure-2.1.8.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\spring-boot-admin-server-2.1.5.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\spring-boot-admin-server-cloud-2.1.5.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\spring-boot-admin-server-ui-2.1.5.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\spring-boot-autoconfigure-2.1.8.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\spring-context-5.1.9.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\spring-core-5.1.9.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\spring-expression-5.1.9.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\spring-jcl-5.1.9.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\spring-web-5.1.9.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\spring-webflux-5.1.9.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\thymeleaf-3.0.11.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\thymeleaf-extras-java8time-3.0.4.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\thymeleaf-spring5-3.0.11.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\unbescape-1.1.6.RELEASE.jar;C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\src\lib\springBootAdminServer\validation-api-2.0.1.Final.jar" com.ndsc.model.ModelApplication

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.8.RELEASE)

2019-12-09 11:28:24.851  INFO 16300 --- [           main] com.ndsc.model.ModelApplication          : Starting ModelApplication on LAPTOP-9LK95335 with PID 16300 (C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot\out\production\offLineSpringBoot started by haoyajun in C:\MyselfApplication\MyProject\IDEA\offLineSpringBoot)
2019-12-09 11:28:24.854  INFO 16300 --- [           main] com.ndsc.model.ModelApplication          : No active profile set, falling back to default profiles: default
2019-12-09 11:28:26.133  INFO 16300 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2019-12-09 11:28:26.686  INFO 16300 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 8099
2019-12-09 11:28:26.689  INFO 16300 --- [           main] com.ndsc.model.ModelApplication          : Started ModelApplication in 2.086 seconds (JVM running for 2.644)
當前線程:reactor-http-nio-2
2019-12-09 11:28:28.889  INFO 16300 --- [ctor-http-nio-2] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
異步線程:task-1
相關文章
相關標籤/搜索