JAVA多线程处理List中的数据

package com.ssl.datasource;

import com.aliyun.odps.*;
import com.aliyun.odps.data.Record;
import com.aliyun.odps.task.SQLTask;
import org.apache.commons.io.output.WriterOutputStream;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ModelSpace {
    static ArrayList<String> tableList = new ArrayList();
    static Odps odpsClient = OdpsUtils.getOdpsClient();
    
    public static void deal() {
        System.out.println("start get table name and save to arraylsit");
        odpsClient.setDefaultProject("xx");
        Tables tables = odpsClient.tables();
        for (Table table : tables) {
            tableList.add(table.getName());
        }
        System.out.println("table name save complete");
        System.out.println("start deal table");
        ArrayList<ArrayList<String>> arrayLists = new ArrayList<>();
        if (tableList.size()<20){
            arrayLists.add(tableList);
        }else {
            for (int i = 0; i < tableList.size(); i++) {
                int num = i /20;
                if (i%20 == 0){
                    arrayLists.add(new ArrayList<String>(20));
                }
                if (arrayLists.size() == num + 1){
                    arrayLists.get(num).add(tableList.get(i));
                }
            }
        }
        //创建线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,10,0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
        for (int i = 0; i < arrayLists.size(); i++) {
            
        }
    }

    public static class UnitThread extends Thread{
        private int pageIndex;
        private ArrayList<ArrayList<String>> arrayLists;

        public UnitThread(int pageIndex, ArrayList<ArrayList<String>> arrayLists) {
            this.pageIndex = pageIndex;
            this.arrayLists = arrayLists;
        }

        @Override
        public void run() {
            System.out.println("thread"+pageIndex+"start");
            String sql = "xxx";
            ArrayList<String> tableList = arrayLists.get(pageIndex);
            for (String table : tableList) {
                try{
                    Instance run = SQLTask.run(odpsClient, sql + table + ";");
                    run.waitForSuccess();
                    List<Record> result = SQLTask.getResult(run);
                    System.out.println(result.get(0).get(0));
                }catch (OdpsException e){
                    e.printStackTrace();
                }
            }
        }
    } 
    public static void main(String[] args) {
        ModelSpace modelSpace = new ModelSpace();
        modelSpace.deal();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值