java 开多个线程等全部线程完成返回结果

发布时间:2024年01月24日

public class ThreadManager {
private static final String TAG = “ThreadManager”;
private CountDownLatch latch;
private int threadCount;
private List datas;
private int average;
private int remainder;
private volatile List results;
private ThreadPoolExecutor mThreadPoolExecutor;
private final long CORE_POOL_SIZE_KEEP_TIME = 120L;
private final LinkedBlockingQueue taskQueue = new LinkedBlockingQueue();
private final WorkTaskUnExecptionHandler mExecptionHandler = new WorkTaskUnExecptionHandler();

public ThreadManager(int threadCount, List<String> datas) {
    this.threadCount = threadCount;
    this.datas = datas;
    average = datas.size() /threadCount;
    remainder = datas.size() % threadCount;
    int latchCount = threadCount;
    if (remainder != 0){
        latchCount= threadCount+1;
    }
    latch = new CountDownLatch(latchCount);
    results = new ArrayList<>();
    mThreadPoolExecutor = new ThreadPoolExecutor(threadCount+1,64+1,CORE_POOL_SIZE_KEEP_TIME,TimeUnit.MILLISECONDS, this.taskQueue, new WorkTaskFactory());
    Log.d(TAG,"threadCount:"+threadCount+"-------"+"datas.size:"+datas.size()+"-------"+"average:"+average);
}

public void startThreads() {
    for (int i = 0; i < threadCount; i++) {
        mThreadPoolExecutor.execute(new Task(i));
    }

    if (remainder != 0){
        mThreadPoolExecutor.execute(new Task(-1));
    }
}

public void awaitAllThreads() throws InterruptedException {
    latch.await();
}

private class Task implements Runnable {
    private int threadId;
    public Task(int threadId) {
        this.threadId = threadId;
    }
    @Override
    public void run() {
        List<String> list = new ArrayList<>();
        if (threadId ==-1){
            int startIndex = threadCount * average;
            for (int i = 0;i<remainder;i++){
                String phone = datas.get(startIndex+1);
               	.......
                list.add(phone);
                }
            }
            
        }else {
            for (int i =0;i<average;i++){
                String phone = datas.get((i * threadId) + i);
                    list.add(phone);

            }
        }

        Log.d(TAG,"list size:"+list.size());
        results.addAll(list);
        latch.countDown(); // 任务完成后,减少计数器
    }
}

public List<String> getResult(){
    return results;
}


class WorkTaskUnExecptionHandler implements Thread.UncaughtExceptionHandler {
    WorkTaskUnExecptionHandler() {
    }

    public void uncaughtException(Thread thread, Throwable ex) {
        ex.printStackTrace();
    }
}

class WorkTaskFactory implements ThreadFactory {
    WorkTaskFactory() {
    }

    public Thread newThread(Runnable r) {
        return this.getWorkThread(r);
    }

    private Thread getWorkThread(Runnable r) {
        Thread task = new Thread(r);
        task.setPriority(5);
        task.setUncaughtExceptionHandler(mExecptionHandler);
        return task;
    }
}

}

文章来源:https://blog.csdn.net/yanwenyuan0304/article/details/135826340
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。