适用场景:
在要处理大批量且无序 的数据 前提下,可使用下面方案,可提高线上服务器吞吐量。以下有场景以及推荐方案。
1、 test 类:
package com.zkbc。。。。。
import java.math.BigDecimal;
import java.util.Date;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import com.zkbc.core.exception.PlatformException;
public class ThreadUtilTest2 {
private static Logger log = LoggerFactory.getLogger(ThreadUtilTest2.class);
//-------------------------------------------------- 测试线程池
@Autowired
private TaskExecutor tasktestExecutor;
@Before
public void init() {
BeanFactoryUtil.init();
tasktestExecutor = (TaskExecutor) BeanFactoryUtil.getContext().getBean("tasktestExecutor");
}
@Test
public void autoTest() {
log.info("step 1 : 自动跑批某个业务,通常会添加个业务开关,设置为可配置。");
log.info("step 2 : 按条件,以及数据量要求上来检索要处理的数据 for循环开线程。");
for (int i = 1; i < 1000; i++) {/**给线程池 开四个线程,1000次循环,如果有空闲线程则可以复用,会调用空闲线程,如没有,加入队列进行排队。*/
try {
tasktestExecutor.execute(new AutoInvest4Original(new BigDecimal("" + i)));
} catch (Exception e) {
log.info("xxxxx异常", e);
}
}
}
public class AutoInvest4Original implements Runnable {
private BigDecimal air;
public AutoInvest4Original(BigDecimal air) {
super();
this.air = air;
}
@Override
public void run() {
Thread current = Thread.currentThread();
log.info("timeStamp:" + DateUtil.formatToYYYY_MM_DDHHMMSS(new Date()) + ",第" + air + "个 , current.getId="
+ current.getId() + ",current.hashCode=" + current.hashCode());
/* 关于线程可以打印很多信息 ,如:"current.getPriority=" + current.getPriority() + ",current.getName=" + current.getName()
+ ",current.activeCount=" + current.activeCount() + ",current.getId=" + current.getId()
+ ",current.getThreadGroup=" + current.getThreadGroup() + ",current.getStackTrace="
+ current.getStackTrace() + ",current.hashCode=" + current.hashCode() + ",current.toString="
+ current.toString() */
log.info("step 3 : 调用要处理数据的大公共代码块,最好加上一段try{} catch(){},这样如果图中处理数据异常,不至于导致当前线程停止 ");
/*for example :
try {
investingByUserRule(air);//eg:该用户投资某个标的,先可用户可投资金额投完才能轮到下个用户,但是标的存在 每次限投限制,还有该用户最多投资限制,每份金额以及最小投资金额.act限制
} catch (PlatformException e) {
log.info("用户【" + air + "】投资异常", e);
}
*/
}
}
/**while循环里用户没钱了,或者用户对于该标的投资完已经不满足继续投资该标的资格,则退出,如果用户还有钱则购买下个标的*/
private void investingByUserRule(BigDecimal air) throws PlatformException {
while (true) {
log.info("step 4 :处理每条数据之前,都要检查下该数据是否已经不满足处理条件或者已经被修改,则continue ");
log.info("step 5 :check要处理的数据entry ,有 则放弃处理,直接break.");
/**if(exp){
* break;
* }
*
* */
}
}
}
2 、 spring注册bean的配置文件添加如下配置:
applicationcontext.xml
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="4"/>
<property name="maxPoolSize" value="4"/>
<property name="queueCapacity" value="3000"/>
<property name="keepAliveSeconds" value="3000"/>
<property name="waitForTasksToCompleteOnShutdown" value="true"/>
<property name="rejectedExecutionHandler">
<bean class="java.util.concurrent.ThreadPoolExecutor$AbortPolicy" />
</property>
</bean>
3、以下一段比较明显,可以看出同一时间会有多个线程并行执行,大大提高服务器效率,也能够看出线程抢占cpu的无序性。
timeStamp:2018-05-30 16:55:29,第979个 , current.getId=85,current.hashCode=2056950446
timeStamp:2018-05-30 16:55:29,第984个 , current.getId=88,current.hashCode=1308138298
timeStamp:2018-05-30 16:55:29,第983个 , current.getId=87,current.hashCode=383362271
timeStamp:2018-05-30 16:55:29,第982个 , current.getId=86,current.hashCode=291955534
timeStamp:2018-05-30 16:55:29,第987个 , current.getId=87,current.hashCode=383362271
timeStamp:2018-05-30 16:55:29,第986个 , current.getId=88,current.hashCode=1308138298
timeStamp:2018-05-30 16:55:29,第985个 , current.getId=85,current.hashCode=2056950446
timeStamp:2018-05-30 16:55:29,第990个 , current.getId=88,current.hashCode=1308138298
timeStamp:2018-05-30 16:55:29,第989个 , current.getId=87,current.hashCode=383362271
timeStamp:2018-05-30 16:55:29,第988个 , current.getId=86,current.hashCode=291955534
timeStamp:2018-05-30 16:55:29,第993个 , current.getId=87,current.hashCode=383362271
timeStamp:2018-05-30 16:55:29,第992个 , current.getId=88,current.hashCode=1308138298
timeStamp:2018-05-30 16:55:29,第991个 , current.getId=85,current.hashCode=2056950446
timeStamp:2018-05-30 16:55:29,第996个 , current.getId=88,current.hashCode=1308138298
timeStamp:2018-05-30 16:55:29,第995个 , current.getId=87,current.hashCode=383362271
timeStamp:2018-05-30 16:55:29,第994个 , current.getId=86,current.hashCode=291955534
timeStamp:2018-05-30 16:55:29,第999个 , current.getId=87,current.hashCode=383362271
timeStamp:2018-05-30 16:55:29,第998个 , current.getId=88,current.hashCode=1308138298
timeStamp:2018-05-30 16:55:29,第997个 , current.getId=85,current.hashCode=2056950446