繁体   English   中英

并非所有Java线程都启动

[英]Not all java threads start

我在oracle数据库中有大量记录,可以为它们完成一些操作。 该应用程序获取4个参数作为输入。 它们是“ Range from”,“ Range to”,“ Thread counts”和“ blockSize”。 通过使用此参数,应用程序可以计算应为每个线程分配多少记录。 线程启动时,它将按blockSize从数据库的blockSize中获取记录,并执行一些操作,然后将记录保存到另一数据库表中。 我在一台8 cpus的计算机上用10,000条记录测试了该应用程序,并将线程数设置为8。没有问题。 在实际环境中,有1,000,000条记录和16 cpus。 通过在那里运行应用程序,并将线程数设置为16或12,某些线程将无法启动。 没有错误或异常消息。 他们只是永远不会跑。 其他线程成功启动。 任何想法?

以下是部分代码准备线程:

List list = new ArrayList();
Object[] records;

int allIDsSize = to - from + 1;

int segmentSize = Math.round(allIDsSize % threadsCount == 0 ? allIDsSize / threadsCount : allIDsSize / threadsCount + 1);

Semaphore semaphore = new Semaphore(0);

List<Future> threads = new ArrayList<Future>();
int k = 0;
while (k * segmentSize < allIDsSize) {
    int from2 = segmentSize * k + 1;

    int to2;
    if (from2 + segmentSize - 1 < allIDsSize) {
        to2 = from2 + segmentSize - 1;
    } else {
        to2 = allIDsSize;
    }

    k++;

    MyThread thread = new MyThread(from + from2 - 1, from + to2 - 1, semaphore); //MyThread implements Callable<String>

    if (log.isInfoEnabled()) {
        log.info(String.format("Thread IDs are from %d to %d", (from + from2 - 1), (from + to2 - 1)));
        log.info("thread started " + k);
    }

    Future<String> future = pool.submit(thread);
    threads.add(future);
}

semaphore.acquire(threads.size());

谢谢。

索尔玛兹

可能是某些线程在所有任务都提交到执行者队列之前完成了,因此执行者服务不必创建最大线程数(固定线程池根据需要创建新线程,直到限制,它不会一次创建所有线程)。

附带说明:将线程概念( threadMyThread )用于确实是一项任务的东西(即RunnableCallable )会令人困惑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM