并非所有 java 线程都启动

发布于 2024-12-11 12:58:12 字数 1433 浏览 0 评论 0原文

我在oracle数据库中有大量记录,需要对它们进行一些操作。 应用程序获取 4 个参数作为输入。它们是“范围从”、“范围到”、“线程计数”和“块大小”。 通过使用此参数,应用程序计算应分配给每个线程的记录数。 当一个线程启动时,它会从数据库blockSize中按blockSize获取记录并进行一些操作,然后将记录保存到数据库的另一个表中。 我在 8 个 cpu 的机器上用 10,000 条记录测试了该应用程序,并将线程数设置为 8。没有问题。 在真实环境中,有1,000,000条记录和16个cpu。通过在那里运行应用程序,并将线程数设置为 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());

谢谢。

索尔马兹。

I have huge amount of records in oracle database that some operations souhld be done for them.
The application gets 4 parameters as input. They are "Range from", "Range to", "Thread counts" and "blockSize".
By using this parameters, the application calculates how many records should be assigned to every thread.
When a thread starts, it fetches records from databasee blockSize by blockSize and does some operations and then, saves records to another table of databse.
I tested the application with 10,000 records in a machine with 8 cpus and set Number of threads to 8. There is no problem.
In real environment, there are 1,000,000 records and 16 cpus. By running the application there, and setting thread counts to 16 or 12,
some threads don't start. There is no error or exception message. They just never
don't run. Other threads start successfully. any idea?

Below is part of the code prepares threads :

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());

Thanks.

Solmaz.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身骄傲 2024-12-18 12:58:12

可能的情况是,某些线程在所有任务提交到执行程序队列之前完成,因此执行程序服务不必创建最大数量的线程(固定线程池根据需要创建新线程,直到达到限制,它不会立即创建所有线程)。

附带说明:令人困惑的是,您将线程概念(threadMyThread)用于真正的任务,(即可运行可调用)。

It is probably the case that some threads finish before all tasks have been submitted to the executor queue, and consequently that the executor service does not have to create the maximum number of threads (fixed thread pool creates new threads as needed up to the limit, it does not create all threads at once).

As a side note: It is confusing that you use the thread concept (thread and MyThread) for something that is really a task, (i.e. Runnableor Callable).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文