1.原始线程总结
我们在使用多线程的时候,往往需要创建Thread类,或者实现Runnable接口,如果要使用到线程池,我们还需要来创建Executors。
2.spring线程
只要要@EnableAsync就可以使用多线程。使用@Async就可以定义一个线程任务。通过spring给我们提供的ThreadPoolTaskExecutor就可以使用线程池。
3.使用
3.1 新建springboot项目
3.2 项目导入idea
目录结构如下:
3.3 新建线程配置类
package com.hotpot.comm.config;
import java.util.concurrent.Executor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/**
* 描述:
* @author: myx
* @date: 2018/1/31 0031
* Copyright © 2017-ganinfo. All rights reserved.
*/
@Configuration
@ComponentScan("com.hotpot.*.service.impl")
@EnableAsync
public class ThreadConfig {
/**
* 执行需要依赖线程池,这里就来配置一个线程池
* @return
*/
@Bean
public Executor getExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(100);
executor.setQueueCapacity(250);
executor.initialize();
return executor;
}
}
3.4 新建service类
package com.hotpot.test.service.impl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.Random;
import java.util.UUID;
/**
* 描述:
* @author: myx
* @date: 2018/1/31 0031 17:09
* @version: V1.0
* Copyright © 2018-ganinfo. All rights reserved.
*/
@Service
public class TestServiceImpl {
/**
* 这里进行标注为异步任务,在执行此方法的时候,会单独开启线程来执行
*/
@Async
public void function1() {
System.out.println("f1 : " + Thread.currentThread().getName() + " " + UUID.randomUUID().toString());
try {
Thread.sleep(new Random().nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Async
public void function2() {
System.out.println("f2 : " + Thread.currentThread().getName() + " " + UUID.randomUUID().toString());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
3.5 编写controler
package com.hotpot.test.controller;
import com.hotpot.test.service.impl.TestServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 描述:
*
* @author: ruikanwang
* @date: 2018/1/31 0031 17:24
* @version: V1.0
* 注意:本内容仅限于新疆感知科技有限公司内部传阅,禁止外泄以及用于其他的商业目
* Copyright © 2018-ganinfo. All rights reserved.
*/
@RestController
public class TestController {
@Autowired
TestServiceImpl service;
@GetMapping("/test")
public void test(){
for (int i = 0; i < 10; i++) {
service.function1(); // 执行异步任务
service.function2();
}
}
}
3.6 启动项目
启动成功访问http://localhost:8080/test 运行结果:
可以看到异步调用实现成功
4.其他实现方式
package com.hotpot.comm.config;
import java.util.concurrent.Executor;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Configuration
@ComponentScan("com.hotpot.*.service.impl")
@EnableAsync
public class ThreadConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(25);
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
参考链接: http://blog.csdn.net/king_kgh/article/details/76022136