快速开启多线程,简单实用

 新建多线程工具类

public class CommonAsynThread<R extends Object> {

	private ExecutorService executor;

	/**
	 * @使用案例 new
	 *       CommonCallableFuture<List<Integer>>(executor).createCallableFuture(()->intList.add(9));
	 * @传入要执行的方法即可:例如:()->intList.add(9)
	 * @param params
	 * @throws ExecutionException
	 * @throws InterruptedException
	 */

	public CommonAsynThread() {
		super();
	}

	public CommonAsynThread(ExecutorService executor) {
		super();
		this.executor = executor;
	}

	public ExecutorService getExecutor() {
		return executor;
	}

	public void setExecutor(ExecutorService executor) {
		this.executor = executor;
	}

	/**
	 * createCallableFuture:没有返回值的异步方法
	 * 
	 * @param jobTaskFuntion
	 * @return
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */

	public void createRunnable(JobExeTaskFuntion jobTaskFuntion)  {
		this.getExecutor().submit(new UserRunnable(jobTaskFuntion));
	}

	/**
	 * createCallableFuture:有返回值的异步方法
	 * 
	 * @param jobTaskFuntion
	 * @return
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */

	public Future<R> createCallableFuture(JobFutureTaskFuntion jobTaskFuntion) throws InterruptedException, ExecutionException {
		Future<R> future = this.getExecutor().submit(new UserCallable(jobTaskFuntion));
		return future;

	}

	/**
	 * UserRunnable:无返回值的线程
	 * 
	 * @author
	 *
	 */
	class UserRunnable implements Runnable {
		private JobExeTaskFuntion jobFuntion;

		public JobExeTaskFuntion getJobFuntion() {
			return jobFuntion;
		}

		public void setJobFuntion(JobExeTaskFuntion jobFuntion) {
			this.jobFuntion = jobFuntion;
		}

		public UserRunnable(JobExeTaskFuntion jobFuntion) {
			super();
			this.jobFuntion = jobFuntion;
		}

		@Override
		public void run() {
			jobFuntion.execute();
		}
	}

	/**
	 * UserCallable:有返回值的线程
	 * 
	 * @author
	 *
	 */
	class UserCallable implements Callable<R> {
		private JobFutureTaskFuntion jobTaskFuntion;

		public JobFutureTaskFuntion getJobTaskFuntion() {
			return jobTaskFuntion;
		}

		public void setJobTaskFuntion(JobFutureTaskFuntion jobTaskFuntion) {
			this.jobTaskFuntion = jobTaskFuntion;
		}

		public UserCallable(JobFutureTaskFuntion jobTaskFuntion) {
			super();
			this.jobTaskFuntion = jobTaskFuntion;
		}

		@Override
		@SuppressWarnings("unchecked")
		public R call() throws Exception {
			return (R) jobTaskFuntion.execute();
		}
	}

	/**
	 * JobExeFuntion:无返回值
	 * 
	 * @author
	 *
	 */
	@FunctionalInterface
	public interface JobExeTaskFuntion {
		void execute();
	}

	/**
	 * JobTaskFuntion:有返回值
	 * 
	 * @author
	 *
	 */
	@FunctionalInterface
	public interface JobFutureTaskFuntion {
		Object execute();
	}

使用多线程工具类

先定义一个线程池

ExecutorService executor = Executors.newFixedThreadPool(lineStationNumListMap.size() * 2);

传入线程池即可使用,看下面

new CommonAsynThread<Object>(executor).createRunnable(() -> {
	//.......你的代码逻辑........
});
// 最后关闭线程池
executor.shutdown();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值