Rumah  >  Soal Jawab  >  teks badan

java - fo循环中增加Runnable线程

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能不能并行,并且帮我改进,跪谢!

阿神阿神2765 hari yang lalu544

membalas semua(2)saya akan balas

  • 黄舟

    黄舟2017-04-18 10:19:32

    Gunakan kumpulan benang!

    private ExecutorService executorService = Executors.newFixedThreadPool(10);
    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);
                executorService.execute(tLbData);
            }
        }
    }

    private ExecutorService executorService = Executors.newFixedThreadPool(10);

    Di atas Executors.newFixedThreadPool(10) mencipta kumpulan benang dengan saiz tetap 10.
    dan kemudian menyerahkannya melalui executorService.execute( Runnable runnable) A tugasan, supaya maksimum 10 tugasan boleh dilaksanakan secara selari Jika lebih banyak utas selari diperlukan, maka parameter Executors.newFixedThreadPool(threadCount) boleh ditetapkan lebih besar.


    Saya ingin mengadu, anda sepatutnya mengelirukan antara muka Runnable dan kelas Thread, bukan? . Pelaksanaan, seperti executorService.execute(runnable) atau Thread(runnable).start().

    balas
    0
  • 怪我咯

    怪我咯2017-04-18 10:19:32

    Kaedah larian tidak boleh dipanggil terus, utas mesti dilaksanakan melalui kaedah Thread.start. Tiada perbezaan antara memanggil kaedah run secara terus dan memanggil kaedah biasa.

    new Thread(tLbData).start()

    balas
    0
  • Batalbalas