SpringBoot--基础--07--多线程

SpringBoot–基础–07–多线程


一、方式1

1.1、代码和测试

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

TaskConfig

package fei.zhou.springboot4.demo2;

import java.util.concurrent.Executor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 描述该类- JPA
 *
 * @author zhoufei
 * @class: TaskConfig
 * @date 2021/1/20 11:38
 * @Verson 1.0 -2021/1/20 11:38
 * @see
 */
@Configuration
@EnableAsync //1 开启异步
public class TaskConfig implements AsyncConfigurer {
	
	@Override
	public Executor getAsyncExecutor() {
		// 设置线程池
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		
		// 核心线程数
		taskExecutor.setCorePoolSize(25);
		// 最大线程数
		taskExecutor.setMaxPoolSize(50);
		// 阻塞队列
		taskExecutor.setQueueCapacity(100);
		taskExecutor.initialize();
		return taskExecutor;
	}
	
	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		return null;
	}
}

DemoService

package fei.zhou.springboot4.demo2;

/**
 * 描述该类- JPA
 *
 * @author zhoufei
 * @class: DemoService
 * @date 2021/1/20 11:42
 * @Verson 1.0 -2021/1/20 11:42
 * @see
 */
public interface DemoService {

	void asyncTask1(Integer i);
}

DemoServiceImpl

package fei.zhou.springboot4.demo2;

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

/**
 * 描述该类- JPA
 *
 * @author zhoufei
 * @class: DemoService
 * @date 2021/1/20 11:42
 * @Verson 1.0 -2021/1/20 11:42
 * @see
 */

@Service
public class DemoServiceImpl implements DemoService {
	
	@Async // 1声明方法为异步方法
	@Override
	public void asyncTask1(Integer i) {
		try {
			//间隔1秒
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("执行异步任务A: " + i);
	}
	
}

DemoController

package fei.zhou.springboot4.demo2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述该类- JPA
 *
 * @author zhoufei
 * @class: DemoService
 * @date 2021/1/20 11:42
 * @Verson 1.0 -2021/1/20 11:42
 * @see
 */

@RestController
public class DemoController {
	@Autowired
	private DemoService demoService;
	
	@RequestMapping("t1")
	public Object t1() {
		for (int i = 1; i < 100; i++) {
			demoService.asyncTask1(i);
		}
		return "ok";
	}
}

二、方式2

2.1、代码和测试

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

DemoService2

package fei.zhou.springboot4.demo3;

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

/**
 * 描述该类- JPA
 *
 * @author zhoufei
 * @class: DemoService
 * @date 2021/1/20 11:42
 * @Verson 1.0 -2021/1/20 11:42
 * @see
 */

@Service
public class DemoService2 {
	
	@Async // 1声明方法为异步方法
	public void asyncTask1(Integer i) {
		try {
			// 间隔1秒
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("执行异步任务A: " + i);
	}
	
}

TaskTest

package fei.zhou.springboot4.demo3;

import java.util.concurrent.Executor;

import fei.zhou.springboot4.demo2.DemoService;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
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;

/**
 * 描述该类- JPA
 *
 * @author zhoufei
 * @class: TaskConfig
 * @date 2021/1/20 11:38
 * @Verson 1.0 -2021/1/20 11:38
 * @see
 */
@Configuration
@ComponentScan("fei.zhou.springboot4.demo3")
@EnableAsync // 1 开启异步
public class TaskTest implements AsyncConfigurer {
	
	@Override
	public Executor getAsyncExecutor() {
		// 设置线程池
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		
		// 核心线程数
		taskExecutor.setCorePoolSize(25);
		// 最大线程数
		taskExecutor.setMaxPoolSize(50);
		// 阻塞队列
		taskExecutor.setQueueCapacity(100);
		taskExecutor.initialize();
		return taskExecutor;
	}
	
	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		return null;
	}
	
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskTest.class);
		DemoService2 demoService = context.getBean(DemoService2.class);
		for (int i = 0; i < 10; i++) {
			demoService.asyncTask1(i);
			
		}
		context.close();
	}
}

代码位置
https://gitee.com/DanShenGuiZu/learnDemo/tree/mysql_mybaties_DB/springboot-learn/springboot-4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值