背景:单线程起动一个loader,loader中分多线程计算
1.单线程起loader,省略
2.多线程loader代码,AbstractDataCounterLoader 启动的线程,run 方法中抽象出calc 让子类实现
2.spring配置
Xml配置例子:
<bean id="mutithreadLoaderTest" class="com.loader.muti.MutiThreadLoader">
<property name="dealTmRecordDao" ref="dealTmRecordDao"/>
<property name="spoutType" value=" mutithreadLoaderTest "/>
<!—主线程的loader频率—>
<property name="clacIntervalMinute" value="1"/>
<!—线程数量—>
<property name="mutiThreadNum" value="10"/>
<!—具体的业务实现类名 这个类需要继承AbstractHandler—>
<property name="handlerClassName" value="com.loader.muti.HandlerTest"/>
</bean>
3.业务实现类需要继承comloader.muti.AbstractHandler 实现execute方法
1.单线程起loader,省略
2.多线程loader代码,AbstractDataCounterLoader 启动的线程,run 方法中抽象出calc 让子类实现
public class MutiThreadLoader extends AbstractDataCounterLoader {
private static final Logger logger = LoggerFactory.getLogger(MutiThreadLoader.class);
/**
* 线程数
*/
private int mutiThreadNum = 0;
/**
* 处理类名
*/
private String handlerClassName;
@Override
protected boolean calc() {
if (mutiThreadNum < 1) {
return false;
}
CountDownLatch countDownLatch = new CountDownLatch(mutiThreadNum);
List<AbstractHandler> mutiThreadLoaders = getMutiThreadLoaders(countDownLatch,
mutiThreadNum);
if (null == mutiThreadLoaders){
return false;
}
ExecutorService exec = Executors.newFixedThreadPool(mutiThreadNum);
for (int i = 0; i < mutiThreadNum; i++) {
exec.submit(mutiThreadLoaders.get(i));
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
logger.error("InterruptedException:", e);
}
exec.shutdown();
System.out.println("end");
return true;
}
protected List<AbstractHandler> getMutiThreadLoaders(CountDownLatch countDownLatch,
int mutiThreadNum) {
List<AbstractHandler> mutiThreadLoaders = new ArrayList<AbstractHandler>();
for (int i = 0; i < mutiThreadNum; i++) {
AbstractHandler handler = getHandler();
if (null == handler){
return null;
}
handler.setThreadNum(i);
handler.setCountDownLatch(countDownLatch);
mutiThreadLoaders.add(handler);
}
return mutiThreadLoaders;
}
// 获得具体的处理类
protected AbstractHandler getHandler(){
AbstractHandler handler = null;
try {
handler = (AbstractHandler) Class.forName(handlerClassName).newInstance();
} catch (InstantiationException e) {
logger.error("getHandler error",e);
} catch (IllegalAccessException e) {
logger.error("getHandler error",e);
} catch (ClassNotFoundException e) {
logger.error("getHandler error",e);
}
return handler;
}
public int getMutiThreadNum() {
return mutiThreadNum;
}
public void setMutiThreadNum(int mutiThreadNum) {
this.mutiThreadNum = mutiThreadNum;
}
public String getHandlerClassName() {
return handlerClassName;
}
public void setHandlerClassName(String handlerClassName) {
this.handlerClassName = handlerClassName;
}
}
2.spring配置
Xml配置例子:
<bean id="mutithreadLoaderTest" class="com.loader.muti.MutiThreadLoader">
<property name="dealTmRecordDao" ref="dealTmRecordDao"/>
<property name="spoutType" value=" mutithreadLoaderTest "/>
<!—主线程的loader频率—>
<property name="clacIntervalMinute" value="1"/>
<!—线程数量—>
<property name="mutiThreadNum" value="10"/>
<!—具体的业务实现类名 这个类需要继承AbstractHandler—>
<property name="handlerClassName" value="com.loader.muti.HandlerTest"/>
</bean>
3.业务实现类需要继承comloader.muti.AbstractHandler 实现execute方法
public class HandlerTest extends AbstractHandler{
@Override
protected void execute(int threadNum) {
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("---" + threadNum);
}
}