以上为线程池的一些配置。
写了一个service,初始化线程池,并且,作为一个调用线程的接口
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import org.springframework.stereotype.Component;@Componentpublic class ThreadPoolTaskService { private ApplicationContext ctx; private ThreadPoolTaskExecutor taskExecutor; public ThreadPoolTaskService () { ctx = new ClassPathXmlApplicationContext("threadpool.xml"); taskExecutor = (ThreadPoolTaskExecutor) ctx.getBean("taskExecutor"); } public void execute(Runnable runnable) { if (null != taskExecutor) { taskExecutor.execute(runnable); } }}
再写一个runnable
public class TesetThread implements Runnable { private Listlist; private TestAService testAService; public TestThread() { } public TestThread(List list, TestAService testAService) { this.list = jobList; this.testAService = testAService; } @Override public void run() { testAService.deal(list); } }
使用的时候
@Autowired
private ThreadPoolTaskService threadPoolTaskService;threadPoolTaskService.execute(new TestThread(list, testAService));