Heim > Fragen und Antworten > Hauptteil
1 我有一个程序需要在for 循环中增加 Runnable,现在发现他执行的顺序是从上往下 ,不是多线程的方式执行
2 代码如下public void addUserACard() {
ThreadLbData tLbData = null; DbBean dbBean = null ; List<DataBase> listData = dataDAO.findHql(" from DataBase where state = '1' order by createDate desc "); DataBase data = null ; if(listData != null && listData.size()> 0){ for (int i = 0; i < listData.size(); i++) { data = listData.get(i); dbBean = createDbBean(data); tLbData = new ThreadLbData(dbBean); tLbData.run(); } } }
@SuppressWarnings("unchecked")public class ThreadLbData implements Runnable {
private DbBean dataBase; private Map map = new HashMap(); private static ConnectionPools connectionPools = ConnectionPools.getInstance(); public ThreadLbData(DbBean dataBase) { this.dataBase = dataBase; } public void run() { Connection conn = connectionPools.getConnectionOracle(); // 获得JDBC链接; try { String lbMaxId = this.getSynLogMaxId(dataBase, conn); Map map = getLbDate(lbMaxId); saveAll((List<ReaderCard>) map.get("listCard"), (List<Userinfo>) map.get("listUser"), conn); } catch (Exception e) { e.printStackTrace(); }finally{ connectionPools.close(null, null, conn); } }
}
3 想让大神帮我看看,线程的run能不能并行,并且帮我改进,跪谢!
三叔2016-11-08 11:09:40
不能直接调用run方法,线程的执行要通过Thread.start方法。直接调用run方法和调用普通方法没有区别。
new Thread(tLbData).start()