springboot 多任务并行执行


@ComponentScan(basePackages = { "com.meadin.agent" })
@EnableApolloConfig
@EnableAsync // 开启多任务
public class BwStudyApplication {

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

配置线程池的参数

@Configuration
@EnableAsync // 开启异步调用 多线程
public class AsyncTaskConfig implements AsyncConfigurer {

	// ThredPoolTaskExcutor的处理流程
	// 当池子大小小于corePoolSize,就新建线程,并处理请求
	// 当池子大小等于corePoolSize,把请求放入workQueue中,池子里的空闲线程就去workQueue中取任务并处理
	// 当workQueue放不下任务时,就新建线程入池,并处理请求,如果池子大小撑到了maximumPoolSize,就用RejectedExecutionHandler来做拒绝处理
	// 当池子的线程数大于corePoolSize时,多余的线程会等待keepAliveTime长时间,如果无请求可处理就自行销毁
	@Override
	public Executor getAsyncExecutor() {
		// 新建一个任务执行器
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		taskExecutor.setCorePoolSize(2);// 设置最小的线程数量
		taskExecutor.setMaxPoolSize(5);// 设置最大的线程数量
		taskExecutor.setQueueCapacity(25);// 等待队列
		taskExecutor.initialize();// 初始化
		return taskExecutor;
	}

	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		return null;
	}

}



同正常的编码一样 编写controller 开放被调用的接口


@RestController
public class Welcome {

	private static String URLS = "http://index.meadin.com/uploads/";

	@Autowired
	AsyncTaskService asynService;

	@Autowired
	AsynTaskFature taskService;

	@RequestMapping("/test")
	public String test1() {
		long s = System.currentTimeMillis();
		for (int i = 0; i < 20; i++) {
			asynService.task1();
			asynService.task2();
			asynService.task3();
			asynService.task4();
			asynService.task5();
		}

		long e = System.currentTimeMillis();
		System.out.println("task总耗时:" + (e - s));
		return "";
	}

单独编写一个多任务执行的类

//多任务执行的类  
@Component
public class AsyncTaskService {
	// 没有返回值的异步调用
	@Async // 表明这异步方法 无返回值
	public void task1() {
		long s = System.currentTimeMillis();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		long e = System.currentTimeMillis();
		System.out.println("tas1k总耗时:" + (e - s) + Thread.currentThread().getName());
		System.out.println("执行异步任务" + 1);
	}

	@Async // 表明这异步方法 无返回值
	public void task2() {
		long s = System.currentTimeMillis();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		long e = System.currentTimeMillis();
		System.out.println("tas2k总耗时:" + (e - s));
		System.out.println("执行异步任务" + 2 + Thread.currentThread().getName());
	}

	@Async // 表明这异步方法 无返回值
	public void task3() {
		long s = System.currentTimeMillis();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		long e = System.currentTimeMillis();
		System.out.println("tas3k总耗时:" + (e - s));
		System.out.println("执行异步任务" + 3 + Thread.currentThread().getName());
	}

	@Async // 表明这异步方法 无返回值
	public void task5() {
		long s = System.currentTimeMillis();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		long e = System.currentTimeMillis();
		System.out.println("tas5k总耗时:" + (e - s));
		System.out.println("执行异步任务" + 5 + Thread.currentThread().getName());
	}

	@Async // 表明这异步方法 无返回值
	public void task4() {
		long s = System.currentTimeMillis();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		long e = System.currentTimeMillis();
		System.out.println("tas4k总耗时:" + (e - s));
		System.out.println("执行异步任务" + 4 + Thread.currentThread().getName());
	}
}

还可以编写带有返回值的异步方法;回调函数

// 多任务执行的类  
@Component
public class AsynTaskFature {
	@Async
	public Future task1() throws InterruptedException {
		long currentTimeMillis = System.currentTimeMillis();
		Thread.sleep(1000);
		long currentTimeMillis1 = System.currentTimeMillis();
		System.out.println("task1任务耗时:" + (currentTimeMillis1 - currentTimeMillis) + "ms");
		return new AsyncResult("task1执行完毕");
	}

	@Async
	public Future task2() throws InterruptedException {
		long currentTimeMillis = System.currentTimeMillis();
		Thread.sleep(2000);
		long currentTimeMillis1 = System.currentTimeMillis();
		System.out.println("task2任务耗时:" + (currentTimeMillis1 - currentTimeMillis) + "ms");
		return new AsyncResult("task2执行完毕");
	}

	@Async
	public Future task3() throws InterruptedException {
		long currentTimeMillis = System.currentTimeMillis();
		Thread.sleep(3000);
		long currentTimeMillis1 = System.currentTimeMillis();
		System.out.println("task3任务耗时:" + (currentTimeMillis1 - currentTimeMillis) + "ms");
		return new AsyncResult("task3执行完毕");
	}

	@Async
	public Future task4() throws InterruptedException {
		long currentTimeMillis = System.currentTimeMillis();
		Thread.sleep(3000);
		long currentTimeMillis1 = System.currentTimeMillis();
		System.out.println("task4任务耗时:" + (currentTimeMillis1 - currentTimeMillis) + "ms");
		return new AsyncResult("task4执行完毕");
	}
}

调用的方法

@RequestMapping("/test1")
	public void test(HttpServletResponse response) throws InterruptedException {
		long s = System.currentTimeMillis();
		Future task1 = taskService.task1();
		Future task2 = taskService.task2();
		Future task3 = taskService.task3();
		for (;;) {
			// 回调函数 Future 如果执行完毕就会返回这个函数
			if (task1.isDone()) {
				System.out.println("task1");
				break;
			}
		}
		long e = System.currentTimeMillis();
		System.out.println("task总耗时:" + (e - s));
	}

你可能感兴趣的:(技术人生)