spring boot学习7-多线程

时间:2021-02-13 23:18:56

前面有一篇java多线程的文章,spring 下实现多线程类似于java下的实现,通过线程池调用多线程实现并发。

首先创建线程池类

/**
 * Created by lengshan on 2018/4/3 0003.
 *
 * 创建线程池类
 * @EnableAsync 创建异步任务
 */
@ComponentScan("com.example.demo")
@Configuration
@EnableAsync //开启异步任务支持
public class Demo_Thread implements AsyncConfigurer{

    //重写getAsyncExcutor()返回一个线程池
    public Executor getAsyncExcutor(){

        //创建线程池
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(5);
        taskExecutor.setMaxPoolSize(10);
        taskExecutor.setQueueCapacity(25);
        return taskExecutor;

    }

}

这个类需要实现AsynConfiguer接口,并且重写了getAsyncExcutor()方法。该方法返回一个类ThreadPoolTaskExecutor类,该类包含线程池类。类似于java线程池的创建,我们需要配置线程池的核心线程数、最大线程数等线程池参数配置。

编写线程调用实例

/**
 * Created by lengshan on 2018/4/3 0003.
 * 异步任务执行类
 */
@Component
public class Demo_Task {

    //创建异步执行方法   @Async注解表明该方法是个异步方法,这里的方法被自动注入ThreadPoolTaskExecutor  作为taskExecutor
    @Async
    public void excuteAsynTask(int i){

        System.out.println("异步执行任务1:"+i+"Thread.currentThread().getName()"+Thread.currentThread().getName());
    }

    public void excueAsynTask2(int i){

        System.out.println("异步执行任务2:"+i+"Thread.currentThread().getName()"+Thread.currentThread().getName());

    }

}

这里我们返回了当前线程的名字

main函数实现调用

@SpringBootApplication
public class DemoSpringexcutortaskApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoSpringexcutortaskApplication.class, args);

		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Demo_Thread.class);

		Demo_Task task =context.getBean(Demo_Task.class);
        //结果是并发执行而不是顺序执行,如果不是放在线程池里面就会顺序执行
		for(int i=0;i<100;i++){
			task.excuteAsynTask(i);
			task.excueAsynTask2(i);
		}
		context.close();

	}
}

简单看一下运行结果

异步执行任务2:0Thread.currentThread().getName()main
异步执行任务1:0Thread.currentThread().getName()SimpleAsyncTaskExecutor-1
异步执行任务2:1Thread.currentThread().getName()main
异步执行任务1:1Thread.currentThread().getName()SimpleAsyncTaskExecutor-2
异步执行任务2:2Thread.currentThread().getName()main
异步执行任务1:2Thread.currentThread().getName()SimpleAsyncTaskExecutor-3
异步执行任务2:3Thread.currentThread().getName()main
异步执行任务1:3Thread.currentThread().getName()SimpleAsyncTaskExecutor-4
异步执行任务2:4Thread.currentThread().getName()main
异步执行任务1:4Thread.currentThread().getName()SimpleAsyncTaskExecutor-5
异步执行任务2:5Thread.currentThread().getName()main
异步执行任务1:5Thread.currentThread().getName()SimpleAsyncTaskExecutor-6
异步执行任务2:6Thread.currentThread().getName()main
异步执行任务1:6Thread.currentThread().getName()SimpleAsyncTaskExecutor-7
异步执行任务2:7Thread.currentThread().getName()main
异步执行任务1:7Thread.currentThread().getName()SimpleAsyncTaskExecutor-8
异步执行任务2:8Thread.currentThread().getName()main
异步执行任务1:8Thread.currentThread().getName()SimpleAsyncTaskExecutor-9
异步执行任务2:9Thread.currentThread().getName()main
异步执行任务1:9Thread.currentThread().getName()SimpleAsyncTaskExecutor-10
异步执行任务2:10Thread.currentThread().getName()main
异步执行任务1:10Thread.currentThread().getName()SimpleAsyncTaskExecutor-11
异步执行任务2:11Thread.currentThread().getName()main
异步执行任务2:12Thread.currentThread().getName()main
异步执行任务1:11Thread.currentThread().getName()SimpleAsyncTaskExecutor-12
异步执行任务2:13Thread.currentThread().getName()main
异步执行任务1:12Thread.currentThread().getName()SimpleAsyncTaskExecutor-13
异步执行任务1:13Thread.currentThread().getName()SimpleAsyncTaskExecutor-14
异步执行任务2:14Thread.currentThread().getName()main
异步执行任务1:14Thread.currentThread().getName()SimpleAsyncTaskExecutor-15
异步执行任务2:15Thread.currentThread().getName()main
异步执行任务1:15Thread.currentThread().getName()SimpleAsyncTaskExecutor-16
异步执行任务2:16Thread.currentThread().getName()main
异步执行任务1:16Thread.currentThread().getName()SimpleAsyncTaskExecutor-17
异步执行任务2:17Thread.currentThread().getName()main
异步执行任务1:17Thread.currentThread().getName()SimpleAsyncTaskExecutor-18
异步执行任务2:18Thread.currentThread().getName()main
异步执行任务1:18Thread.currentThread().getName()SimpleAsyncTaskExecutor-19
异步执行任务2:19Thread.currentThread().getName()main
异步执行任务1:19Thread.currentThread().getName()SimpleAsyncTaskExecutor-20
异步执行任务2:20Thread.currentThread().getName()main
异步执行任务2:21Thread.currentThread().getName()main
异步执行任务2:22Thread.currentThread().getName()main
异步执行任务1:20Thread.currentThread().getName()SimpleAsyncTaskExecutor-21
异步执行任务2:23Thread.currentThread().getName()main
异步执行任务1:23Thread.currentThread().getName()SimpleAsyncTaskExecutor-24
异步执行任务1:24Thread.currentThread().getName()SimpleAsyncTaskExecutor-25
异步执行任务2:24Thread.currentThread().getName()main
异步执行任务1:22Thread.currentThread().getName()SimpleAsyncTaskExecutor-23
异步执行任务1:21Thread.currentThread().getName()SimpleAsyncTaskExecutor-22
异步执行任务1:25Thread.currentThread().getName()SimpleAsyncTaskExecutor-26
异步执行任务2:25Thread.currentThread().getName()main
异步执行任务2:26Thread.currentThread().getName()main
异步执行任务2:27Thread.currentThread().getName()main
异步执行任务2:28Thread.currentThread().getName()main
异步执行任务1:26Thread.currentThread().getName()SimpleAsyncTaskExecutor-27
异步执行任务1:27Thread.currentThread().getName()SimpleAsyncTaskExecutor-28
异步执行任务2:29Thread.currentThread().getName()main
异步执行任务1:29Thread.currentThread().getName()SimpleAsyncTaskExecutor-30
异步执行任务1:28Thread.currentThread().getName()SimpleAsyncTaskExecutor-29
异步执行任务2:30Thread.currentThread().getName()main
异步执行任务1:30Thread.currentThread().getName()SimpleAsyncTaskExecutor-31
异步执行任务2:31Thread.currentThread().getName()main
异步执行任务1:31Thread.currentThread().getName()SimpleAsyncTaskExecutor-32
异步执行任务2:32Thread.currentThread().getName()main
异步执行任务2:33Thread.currentThread().getName()main
异步执行任务2:34Thread.currentThread().getName()main
异步执行任务1:32Thread.currentThread().getName()SimpleAsyncTaskExecutor-33
异步执行任务1:33Thread.currentThread().getName()SimpleAsyncTaskExecutor-34
异步执行任务2:35Thread.currentThread().getName()main
异步执行任务1:35Thread.currentThread().getName()SimpleAsyncTaskExecutor-36
异步执行任务1:34Thread.currentThread().getName()SimpleAsyncTaskExecutor-35
异步执行任务2:36Thread.currentThread().getName()main
异步执行任务1:36Thread.currentThread().getName()SimpleAsyncTaskExecutor-37
异步执行任务2:37Thread.currentThread().getName()main
异步执行任务1:37Thread.currentThread().getName()SimpleAsyncTaskExecutor-38
异步执行任务2:38Thread.currentThread().getName()main
异步执行任务2:39Thread.currentThread().getName()main
异步执行任务1:38Thread.currentThread().getName()SimpleAsyncTaskExecutor-39
异步执行任务2:40Thread.currentThread().getName()main
异步执行任务1:40Thread.currentThread().getName()SimpleAsyncTaskExecutor-41
异步执行任务1:39Thread.currentThread().getName()SimpleAsyncTaskExecutor-40
异步执行任务1:41Thread.currentThread().getName()SimpleAsyncTaskExecutor-42
异步执行任务2:41Thread.currentThread().getName()main
异步执行任务2:42Thread.currentThread().getName()main
异步执行任务2:43Thread.currentThread().getName()main
异步执行任务1:42Thread.currentThread().getName()SimpleAsyncTaskExecutor-43
异步执行任务2:44Thread.currentThread().getName()main
异步执行任务2:45Thread.currentThread().getName()main
异步执行任务2:46Thread.currentThread().getName()main
异步执行任务2:47Thread.currentThread().getName()main
异步执行任务2:48Thread.currentThread().getName()main
异步执行任务1:44Thread.currentThread().getName()SimpleAsyncTaskExecutor-45
异步执行任务1:45Thread.currentThread().getName()SimpleAsyncTaskExecutor-46
异步执行任务2:49Thread.currentThread().getName()main
异步执行任务2:50Thread.currentThread().getName()main
异步执行任务2:51Thread.currentThread().getName()main
异步执行任务1:43Thread.currentThread().getName()SimpleAsyncTaskExecutor-44
异步执行任务2:52Thread.currentThread().getName()main
异步执行任务2:53Thread.currentThread().getName()main
异步执行任务2:54Thread.currentThread().getName()main
异步执行任务2:55Thread.currentThread().getName()main
异步执行任务1:46Thread.currentThread().getName()SimpleAsyncTaskExecutor-47
异步执行任务2:56Thread.currentThread().getName()main
异步执行任务2:57Thread.currentThread().getName()main
异步执行任务2:58Thread.currentThread().getName()main
异步执行任务1:47Thread.currentThread().getName()SimpleAsyncTaskExecutor-48
异步执行任务2:59Thread.currentThread().getName()main
异步执行任务1:48Thread.currentThread().getName()SimpleAsyncTaskExecutor-49
异步执行任务2:60Thread.currentThread().getName()main
异步执行任务2:61Thread.currentThread().getName()main
异步执行任务2:62Thread.currentThread().getName()main
异步执行任务1:51Thread.currentThread().getName()SimpleAsyncTaskExecutor-52
异步执行任务2:63Thread.currentThread().getName()main
异步执行任务1:52Thread.currentThread().getName()SimpleAsyncTaskExecutor-53
异步执行任务2:64Thread.currentThread().getName()main
异步执行任务2:65Thread.currentThread().getName()main
异步执行任务1:53Thread.currentThread().getName()SimpleAsyncTaskExecutor-54
异步执行任务2:66Thread.currentThread().getName()main
异步执行任务1:54Thread.currentThread().getName()SimpleAsyncTaskExecutor-55
异步执行任务1:56Thread.currentThread().getName()SimpleAsyncTaskExecutor-57
异步执行任务2:67Thread.currentThread().getName()main
异步执行任务1:57Thread.currentThread().getName()SimpleAsyncTaskExecutor-58
异步执行任务2:68Thread.currentThread().getName()main
异步执行任务2:69Thread.currentThread().getName()main
异步执行任务1:59Thread.currentThread().getName()SimpleAsyncTaskExecutor-60
异步执行任务2:70Thread.currentThread().getName()main
异步执行任务2:71Thread.currentThread().getName()main
异步执行任务1:49Thread.currentThread().getName()SimpleAsyncTaskExecutor-50
异步执行任务1:55Thread.currentThread().getName()SimpleAsyncTaskExecutor-56
异步执行任务2:72Thread.currentThread().getName()main
异步执行任务1:50Thread.currentThread().getName()SimpleAsyncTaskExecutor-51
异步执行任务2:73Thread.currentThread().getName()main
异步执行任务2:74Thread.currentThread().getName()main
异步执行任务2:75Thread.currentThread().getName()main
异步执行任务2:76Thread.currentThread().getName()main
异步执行任务1:60Thread.currentThread().getName()SimpleAsyncTaskExecutor-61
异步执行任务1:58Thread.currentThread().getName()SimpleAsyncTaskExecutor-59
异步执行任务2:77Thread.currentThread().getName()main
异步执行任务1:61Thread.currentThread().getName()SimpleAsyncTaskExecutor-62
异步执行任务2:78Thread.currentThread().getName()main
异步执行任务1:62Thread.currentThread().getName()SimpleAsyncTaskExecutor-63
异步执行任务2:79Thread.currentThread().getName()main
异步执行任务2:80Thread.currentThread().getName()main
异步执行任务1:63Thread.currentThread().getName()SimpleAsyncTaskExecutor-64
异步执行任务2:81Thread.currentThread().getName()main
异步执行任务1:65Thread.currentThread().getName()SimpleAsyncTaskExecutor-66
异步执行任务2:82Thread.currentThread().getName()main
异步执行任务1:66Thread.currentThread().getName()SimpleAsyncTaskExecutor-67
异步执行任务2:83Thread.currentThread().getName()main
异步执行任务2:84Thread.currentThread().getName()main
异步执行任务1:69Thread.currentThread().getName()SimpleAsyncTaskExecutor-70
异步执行任务2:85Thread.currentThread().getName()main
异步执行任务1:71Thread.currentThread().getName()SimpleAsyncTaskExecutor-72
异步执行任务1:72Thread.currentThread().getName()SimpleAsyncTaskExecutor-73
异步执行任务2:86Thread.currentThread().getName()main
异步执行任务2:87Thread.currentThread().getName()main
异步执行任务2:88Thread.currentThread().getName()main
异步执行任务1:73Thread.currentThread().getName()SimpleAsyncTaskExecutor-74
异步执行任务1:68Thread.currentThread().getName()SimpleAsyncTaskExecutor-69
异步执行任务2:89Thread.currentThread().getName()main
异步执行任务1:64Thread.currentThread().getName()SimpleAsyncTaskExecutor-65
异步执行任务1:74Thread.currentThread().getName()SimpleAsyncTaskExecutor-75
异步执行任务2:90Thread.currentThread().getName()main
异步执行任务2:91Thread.currentThread().getName()main
异步执行任务1:75Thread.currentThread().getName()SimpleAsyncTaskExecutor-76
异步执行任务2:92Thread.currentThread().getName()main
异步执行任务1:76Thread.currentThread().getName()SimpleAsyncTaskExecutor-77
异步执行任务2:93Thread.currentThread().getName()main
异步执行任务1:78Thread.currentThread().getName()SimpleAsyncTaskExecutor-79
异步执行任务1:77Thread.currentThread().getName()SimpleAsyncTaskExecutor-78
异步执行任务2:94Thread.currentThread().getName()main
异步执行任务1:67Thread.currentThread().getName()SimpleAsyncTaskExecutor-68
异步执行任务2:95Thread.currentThread().getName()main
异步执行任务1:70Thread.currentThread().getName()SimpleAsyncTaskExecutor-71
异步执行任务2:96Thread.currentThread().getName()main
异步执行任务2:97Thread.currentThread().getName()main
异步执行任务2:98Thread.currentThread().getName()main
异步执行任务1:79Thread.currentThread().getName()SimpleAsyncTaskExecutor-80
异步执行任务2:99Thread.currentThread().getName()main
2018-04-10 09:15:00.359  INFO 3980 --- [           main] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1a6f2363: startup date [Tue Apr 10 09:14:59 CST 2018]; root of context hierarchy
异步执行任务1:82Thread.currentThread().getName()SimpleAsyncTaskExecutor-83
异步执行任务1:83Thread.currentThread().getName()SimpleAsyncTaskExecutor-84
2018-04-10 09:15:00.360  INFO 3980 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
异步执行任务1:85Thread.currentThread().getName()SimpleAsyncTaskExecutor-86
异步执行任务1:87Thread.currentThread().getName()SimpleAsyncTaskExecutor-88
异步执行任务1:88Thread.currentThread().getName()SimpleAsyncTaskExecutor-89
异步执行任务1:80Thread.currentThread().getName()SimpleAsyncTaskExecutor-81
异步执行任务1:81Thread.currentThread().getName()SimpleAsyncTaskExecutor-82
异步执行任务1:86Thread.currentThread().getName()SimpleAsyncTaskExecutor-87
异步执行任务1:84Thread.currentThread().getName()SimpleAsyncTaskExecutor-85
异步执行任务1:95Thread.currentThread().getName()SimpleAsyncTaskExecutor-96
异步执行任务1:92Thread.currentThread().getName()SimpleAsyncTaskExecutor-93
异步执行任务1:91Thread.currentThread().getName()SimpleAsyncTaskExecutor-92
异步执行任务1:93Thread.currentThread().getName()SimpleAsyncTaskExecutor-94
异步执行任务1:90Thread.currentThread().getName()SimpleAsyncTaskExecutor-91
异步执行任务1:89Thread.currentThread().getName()SimpleAsyncTaskExecutor-90
异步执行任务1:96Thread.currentThread().getName()SimpleAsyncTaskExecutor-97
异步执行任务1:98Thread.currentThread().getName()SimpleAsyncTaskExecutor-99
异步执行任务1:97Thread.currentThread().getName()SimpleAsyncTaskExecutor-98
异步执行任务1:94Thread.currentThread().getName()SimpleAsyncTaskExecutor-95
异步执行任务1:99Thread.currentThread().getName()SimpleAsyncTaskExecutor-100
参考:《Java EE开发的颠覆者 spring boot实战》