fo循环中增加Runnable线程

2016/12 01 13:12
此文章不得转载,如有需要请联系网站管理员。

一度的温暖,一百度的爱情,来自于胜博发娱乐老虎机的点点滴滴

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 ;
    iflistData != null && listData.size> 0{
        for int i = 0; i < listData.size; i++ {
            data = listData.geti;
            dbBean =  createDbBeandata;
            tLbData = new ThreadLbDatadbBean;
            tLbData.run;
        }
    }
}

}

@SuppressWarnings”unchecked”
public class ThreadLbData implements Runnable {

private DbBean dataBase;
private Map map = new HashMap;
private static ConnectionPools connectionPools = ConnectionPools.getInstance;

public ThreadLbDataDbBean dataBase {
    this.dataBase = dataBase;
}



public void run {
    
    Connection conn = connectionPools.getConnectionOracle; // 获得JDBC链接;
    try {
        String lbMaxId = this.getSynLogMaxIddataBase, conn;
        Map map = getLbDatelbMaxId;
        saveAllList<ReaderCard> map.get"listCard", List<Userinfo>   map.get"listUser", conn;
    } catch Exception e {
        e.printStackTrace;
    }finally{
        connectionPools.closenull, null, conn;
    }
}

}

3 想让大神帮我看看,线程的run能不克不及并行,并且帮我改进,跪谢!

用线程池呗!

private ExecutorService executorService = Executors.newFixedThreadPool10;
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 ;
    iflistData != null && listData.size> 0{
        for int i = 0; i < listData.size; i++ {
            data = listData.geti;
            dbBean =  createDbBeandata;
            executorService.executetLbData;
        }
    }
}

private ExecutorService executorService = Executors.newFixedThreadPool10;

下面的 Executors.newFixedThreadPool10 是创立了一个结实大小为10的线程池.
然后经过 executorService.execute Runnable runnable 的方法提交一个任务, 如许可以最大并行地实行10个任务. 假如需求并行的线程更多, 那么 Executors.newFixedThreadPoolthreadCount 的参数可以设置大一些.


我要吐槽一下, 你应该是把 Runnable 接口和 Thread 类稠浊了吧? 完成 Runnable 接口的类不代表它便是在一个新的线程中运转, 你必需表现地地将 Runnable 提交到新线程中实行, 比方 executorService.executerunnable 或 new Threadrunnable.start.

不克不及直接调用run办法,线程的实行要经过Thread.start办法。直接调用run办法和调用平凡办法没有区别。

new ThreadtLbData.start

(看完/读完)这篇文章有何感想! 来看看胜博发娱乐老虎机是怎么评论的吧!

--转载请注明: sbf999胜博发老虎机_胜博发娱乐老虎机_sbf999胜博发pt娱乐老虎机 » fo循环中增加Runnable线程

发表评论

(必填)