java_多线程调用笔记1
1.
/**
*调用方法
*/
public void datas(DataCont vo) throws Exception {
String threadGroup = "数据1"+new Date(); // 设置线程组名()
try {
List<Datas> list = this.getDataList(vo);
int threadCount = list.size(); // 设置最大线程数
ThreadTools.initThreadGroup(threadGroup, threadCount); // 初始化线程组
Iterator<Datas> iterator = list.iterator();
while (iterator.hasNext()) {
Datas datas = iterator.next();
DatasCall call = new DatasCall(datas,sysFlag);
ThreadTools.addThread(call, threadGroup);
iterator.remove();
}
ThreadTools.listenThreadEnd(threadGroup);
} catch (Exception e) {
throw new YssException("失败!");
} finally {
ThreadTools.cancelGroup(threadGroup);
}
}
2.
public class DatasCall implements Callable<String> {
private Datas datas = null; //数据迁移对象
private String sysFlag = ""; //连接池标示
public DatasCall(Datas datas, String sysFlag) throws Exception{
this.datas = datas;
this.sysFlag = sysFlag;
}
/**
* @desc 开启线程方法
* @return
* @throws Exception
*/
public String call() throws Exception {
Tables tables = DatasFactory.createTables(datas.getTabName(),datas.getDataSource());
try{
tables.init(datas,sysFlag);
tables.moveData();
}catch (Exception e) {
throw new Exception("出错:"+e);
}finally{
tables.close();
}
return "ok!";
}
}
3.
/**
* 根据表名获取数据迁移实现类
*
*/
public class DatasFactory {
public static Tables createTables(String tabName, String dataSource)
throws Exception {
Tables tables = null;
//执行代码
return tableMove;
}
4.
/**
* 数据接口
*/
public abstract class Tables {
private static Logger logger = Logger.getLogger(Tables.class);
protected Datas datas;
protected Connection conn;
protected DbTemplate dbTemplate;
protected TransactionTemplate transactionTemplate;
protected static final Integer count=100000;//每10万条存入一次数据库
//数据
public abstract void moveData() throws Exception;
//数据转换
public abstract Object transObj(ResultSet rs,Object... args) throws Exception;
//初始化实现类
public void init(Datas datas, String sysFlag) throws Exception{
this.datas=datas;
this.conn = ConnectionManager.getConnection(sysFlag);
this.dbTemplate=(DbTemplate)SpringContextHolder.getBean("dbTemplate");
this.transactionTemplate=(TransactionTemplate)SpringContextHolder.getBean("transactionTemplate");
}
//关闭数据库连接
public void close() throws Exception{
ConnectionManager.closeConnection();
}
/**
* 提取数据库关闭结果集操作的公共模块
* @param dbFlag 数据库标志
* @param action ResultClose 需关闭的结果集
* @throws SQLException
*/
public final static void execute(ResultClose action) throws Exception{
ResultSet rs = null;
try {
rs = action.doInStatement(rs);
}catch (Exception e) {
logger.error(e.getMessage());
throw e;
}finally {
if(rs!=null){
Statement st = rs.getStatement();
rs.close();
rs = null;
st.close();
st = null;
}
}
}
public Datas getDatas() {
return datas;
}
public void setDatas(Datas datas) {
this.datas = datas;
}
public Connection getConn() {
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
public DbTemplate getDbTemplate() {
return dbTemplate;
}
public void setDbTemplate(DbTemplate dbTemplate) {
this.dbTemplate = dbTemplate;
}
public TransactionTemplate getTransactionTemplate() {
return transactionTemplate;
}
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
}
线程管理工具ThreadTools见下文

浙公网安备 33010602011771号