Java中如何实现多线程

发布于 2024-12-13 05:50:35 字数 153 浏览 2 评论 0原文

我必须对一个以 1000 个批次运行代码的方法进行多线程处理。我需要将这些批次分配给不同的线程。

目前我已经生成了 3 个线程,但所有 3 个线程都选择了第一批 1000 个线程。 我希望其他批次不应该选择同一批次,而是选择其他批次。

请大家帮忙并提出建议。

I have to multithread a method that runs a code in the batches of 1000 . I need to give these batches to different threads .

Currently i have spawn 3 threads but all 3 are picking the 1st batch of 1000 .
I want that the other batches should not pick the same batch instead pick the other batch .

Please help and give suggestions.

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

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

发布评论

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

评论(4

初雪 2024-12-20 05:50:35

我会使用 ExecutorService

int numberOfTasks = ....
int batchSize = 1000;
ExecutorService es = Executors.newFixedThreadPool(3);
for (int i = 0; i < numberOfTasks; i += batchSize) {
    final int start = i;
    final int last = Math.min(i + batchSize, numberOfTasks);
    es.submit(new Runnable() {
        @Override
        public void run() {
            for (int j = start; j < last; j++)
                System.out.println(j); // do something with j
        }
    });
}
es.shutdown();

I would use an ExecutorService

int numberOfTasks = ....
int batchSize = 1000;
ExecutorService es = Executors.newFixedThreadPool(3);
for (int i = 0; i < numberOfTasks; i += batchSize) {
    final int start = i;
    final int last = Math.min(i + batchSize, numberOfTasks);
    es.submit(new Runnable() {
        @Override
        public void run() {
            for (int j = start; j < last; j++)
                System.out.println(j); // do something with j
        }
    });
}
es.shutdown();
谈情不如逗狗 2024-12-20 05:50:35

将批次放入 BlockingQueue并使工作线程从队列中获取批次。

Put the batches in a BlockingQueue and make your worker threads to take the batches from the queue.

七分※倦醒 2024-12-20 05:50:35

检索批次时使用互斥体。这样,线程就无法同时访问临界区,也不会意外访问同一批。

我假设您在线程选择批次后将其删除。

编辑:aioobe 和 jonas 的答案更好,使用它。这是一个替代方案。 :)

Use a lock or a mutex when retrieving the batch. That way, the threads can't access the critical section at the same time and can't accidentally access the same batch.

I'm assuming you're removing a batch once it was picked by a thread.

EDIT: aioobe and jonas' answers are better, use that. This is an alternative. :)

故事未完 2024-12-20 05:50:35

您需要同步对批次中作业列表的访问。 (“同步”本质上意味着“确保线程意识到潜在的竞争条件”。在大多数情况下,这意味着“让某个方法一次由一个线程执行”。)

这是最容易解决的问题,使用 java .util.concurrent 包。查看 的各种实现BlockingQueue 例如 ArrayBlockingQueueLinkedBlockingQueue

You need to synchronize the access to the list of jobs in the batch. ("Synchronize" essentially means "make sure the threads aware of potential race-conditions". In most scenarios this means "let some method be executed by a single thread at a time".)

This is easiest solved using the java.util.concurrent package. Have a look at the various implementations of BlockingQueue for instance ArrayBlockingQueue or LinkedBlockingQueue.

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