如果看完觉得对您有帮助到,麻烦关注一下,您的关注是我继续更新的动力。谢谢!!
声明变量
private static ThreadLocal<Map<String, Connection>> threadLocal = new ThreadLocal<Map<String,Connection>>();
private static final int MAX_THREAD = 10;
private static Stack<Integer> yw_shool_ids_stack = new Stack<Integer>();
main方法
public static void main(String[] args) throws SQLException {
Set<Integer> ids = DBUtil.getIds(local_conn, "select user_id from yw_all_school where is_new = 0 and has_camerer=0");//老系统且无摄像头的学校
System.out.println("幼儿云中符合既是老系统数据又是无摄像头的园所一共有"+ids.size()+"所学校");
logger.info("幼儿云中符合既是老系统数据又是无摄像头的园所一共有"+ids.size()+"所学校");
initSchoolIdsStack(ids);//需要实现多线程的方法
for(int i = 0;i< MAX_THREAD;i++){
DataTrf t = new DataTrf();
t.start();
}
}
初始化栈
private static void initSchoolIdsStack(Set<Integer> yw_school_ids){
int count=0;
if(yw_school_ids != null && yw_school_ids.size() >0){
for (Integer yw_school_id : yw_school_ids) {
//判断当前园所是否缺少班级或缺少孩子或缺少老师或手机号重复数量大于20
int yw_school_duplicate_num = (int) yw_get_all_user_num(yw_school_id).get("yw_school_duplicate_num");//该园所重复手机号数量
boolean is_export = isExport(yw_school_id);//返回true为可导数据,false为不可导数据
if(is_export&&yw_school_duplicate_num<20&&yw_school_id!=367&&yw_school_id!=347&&yw_school_id!=975){
yw_shool_ids_stack.add(yw_school_id);
count++;
}
}
System.out.println(count);
}
}
重写run方法
@Override
public void run(){
Map<String, Connection> connection = getConnection();
System.out.println(connection);
while(yw_shool_ids_stack != null && !yw_shool_ids_stack.isEmpty()){
int yw_school_id = yw_shool_ids_stack.pop();
try {
System.out.println("当前导入学校为园所school_id= "+yw_school_id);
importSchoolData(yw_school_id);
} catch (Exception e) {
logger.info("导数据时出现异常的学校school_id = "+yw_school_id);
e.printStackTrace();
}
}
close();
}
获取数据库连接
private static Map<String, Connection> getConnection(){
Map<String, Connection> connectionManager = null;
if(threadLocal != null){
connectionManager = threadLocal.get();
if(connectionManager == null){
connectionManager = new HashMap<String,Connection>();
connectionManager.put(Constant.LOCAL_CONN, DBUtil.getConn(Constant.JDBC_LOCAL));
connectionManager.put(Constant.YW_REAL_CONN, DBUtil.getYwConn());
connectionManager.put(Constant.BBT_MASTER_CONN, DBUtil.getConn(Constant.JDBC_LOCAL));
threadLocal.set(connectionManager);
}
}
return connectionManager;
}
数据连接关闭
private static void close(){
if(threadLocal != null){
Map<String, Connection> connectionManager = threadLocal.get();
if(connectionManager != null){
Connection local_conn = connectionManager.get(Constant.LOCAL_CONN);
if(local_conn != null){
try {
local_conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
Connection bbt_master_conn = connectionManager.get(Constant.BBT_MASTER_CONN);
if(bbt_master_conn != null){
try {
bbt_master_conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
Connection yw_real_conn = connectionManager.get(Constant.YW_REAL_CONN);
if(yw_real_conn != null){
try {
yw_real_conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
threadLocal.remove();;
}
}
}