1.
ThreadTest.java
com.hand.hmall.job; org.springframework.context.ApplicationContext; org.springframework.context.support.ClassPathXmlApplicationContext; org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; java.util.ArrayList; java.util.List; java.util.concurrent.ExecutionException; java.util.concurrent.Future; java.util.concurrent.atomic.AtomicInteger; ThreadTest { main(String[] args) { String ret = ThreadTest().doBuiness(); System..println(ret); } String doBuiness() { AtomicInteger taskCounter = AtomicInteger(); Integer pageLoad = ; Integer maxThread = ; List<Future<String>> results = ArrayList<>(); ApplicationContext ctx = ClassPathXmlApplicationContext(); ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor) ctx.getBean(); List<String> list = ArrayList<>(); (i = ; i < ; i++) { list.add(i + ); } List<String> tempList = ArrayList<>(); (String s : list) { tempList.add(s); (tempList.size() == pageLoad) { results.add(ThreadTest().doTask(taskCounter, ArrayList<>(tempList), taskExecutor, maxThread)); tempList.clear(); } } (!tempList.isEmpty()) { results.add(ThreadTest().doTask(taskCounter, ArrayList<>(tempList), taskExecutor, maxThread)); tempList.clear(); } (Future<String> result : results) { { String re = result.get(); System..println(re); } (InterruptedException | ExecutionException e) { e.printStackTrace(); } } ; } Future<String> doTask(AtomicInteger taskCounter, List<String> names, ThreadPoolTaskExecutor taskExecutor, Integer maxThread) { TenantAwareCallable<String> taskRunner = TenantAwareCallable<String>() { String performActualWork() { .incrementAndGet(); { System..println(); { Thread.(); } (InterruptedException e) { e.printStackTrace(); } ; } (Exception e) { ; } { .decrementAndGet(); System..println(+ .get()); } } }; () { (taskCounter.get() <= maxThread) { taskExecutor.submit(taskRunner); } } } }
2.
TenantAwareCallable.java
com.hand.hmall.job; java.util.concurrent.Callable; TenantAwareCallable<> Callable<> { call() Exception { performActualWork(); } performActualWork(); }
3.
spring-mybatis.xml
<!--線程池2-->
<bean id="taskPool"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="32"/>
<property name="maxPoolSize" value="64"/>
<property name="queueCapacity" value="1024"/>
<property name="keepAliveSeconds" value="30"/>
<property name="waitForTasksToCompleteOnShutdown" value="true"/>
</bean>