0

我有以下代码

private List<String[]> userList2 = new ArrayList<String[]>(10000);

ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 10, TimeUnit.SECONDS, 
new ArrayBlockingQueue<Runnable>(5), new ThreadPoolExecutor.CallerRunsPolicy());

数据库查询

while (rs.next()) 
{
            data = new String[2];
            data[0] = rs.getString("userid");
            data[1] = rs.getString("email");
            userList2.add(data);

           if(userList2.size()==10000)  //Confusion in this part..
           {

            final List<String[]> elist = new ArrayList<String[]>(userList2);
            executor.execute(new Runnable() {
                public void run() {

                doBilling(con,elist);  //Parallel is not happening here... 

            }
            });

我有一个方法

doBillng(Connection con,List<String[]> userList)
{

String list[]=null; 
String userid=" ";
 for(int i=0;i<userList.size();i++)
    {
        list=userList.get(i);
        userid=props[0];
        list = BillingDao.billById(uid, con);

    }
}

当 userlist2 大小为 10000 时,我想在 10 个线程中并行运行 doBilling,以便它可以以极快的速度达到 10000 条记录。但它没有发生!请建议我做错了什么以及应该如何解决..

提前致谢

4

1 回答 1

0

您只向 提交了一项任务,并且在返回executor时会发生这种情况。因此,您可以运行一个来处理 ArrayList 的 10000 个元素!!!!!!!if(userList2.size()==10000)true Thread

如果你想让 10 个线程执行 ArrayList 的 10000 个元素,你的代码应该是这样的:

while (rs.next()) 
{
    data = new String[2];
    data[0] = rs.getString("userid");
    data[1] = rs.getString("email");
    userList2.add(data);
    if(userList2.size()% 1000 == 0)  //Check if size is multiple of 1000(obtained by 10000/10)
    {
        final List<String[]> elist = new ArrayList<String[]>(userList2);
        executor.execute(new Runnable() 
        {
            public void run() 
            {
                doBilling(con,elist);  //create new ArrayList and assign it to a seperate thread.
            }
        });
        userList2.clear();//clearing the arrayList so that next time when submitted to executor , new elements are processed.
    }
}
于 2013-02-22T22:43:14.030 回答