多线程 - 分配工作
Multi-threading - Allocating Work
在循环中创建和使用线程的正确方法是什么?
如果我想处理一百万个项目,我将创建一个循环来执行此操作。因此,为了效率起见,我将使其成为多线程并将每个项目分配给一个线程。假设我创建了五个线程来处理这个问题。如何将工作分配给线程?
如果一个线程有一个较小的项目要处理,那么它可以自由处理另一个 - 我如何才能将循环中的下一个项目分配给该线程?
我是否在循环外创建线程然后在循环内使用它们?
这是我正在处理的一个示例 – 它缺少要使用的对象的创建并且只使用了两个线程,但我认为这里的人足够聪明,知道我的意思:)
public class App
{
public static void main( String[] args )
{
App a = new App();
a.doTest();
}
private void doTest() {
Count c = new Count(21);
Count c2 = new Count(7);
Thread t = new Thread(c);
Thread t2 = new Thread(c2);
t.start();
t2.start();
for (int f = 0; f < 10; f++) {
//here - how do I select which thread to send the work to?
// ?????.processThis(nextObject); //how do I send this to the right thread (one that is idle or has least work?)
}
}
public class Count implements Runnable {
private int count;
public void processThis(Object someItemToProcess) {
//here I'll set the object to process and call the method to process it
}
public Count(int count) {
this.count = count;
}
@Override
public void run() {
//for illustration purposes
// for (int i = 1; i < count; i++) {
// System.out.println("Thread " + Thread.currentThread().getId() + " Count = " + i);
// }
}
}
}
此类问题通常通过线程池来解决,您可以将任务提供给线程池并等待完成。在Java中,有几种线程池的实现,你可以从一个固定大小的开始学习:
// Create a thread pool
ExecutorService executor = Executors.newFixedThreadPool(threadsCount);
// Submit your tasks to the pool:
for (Count c: myCounts) {
executor.submit(c);
}
// Shutdown the pool when you don't need it
executor.shutdown();
池将负责并行执行您的任务。如果您需要控制任务的执行、等待它们完成、取消它们、获取它们的工作结果等,只需使用 submit()
方法返回的 Future
对象。您可以在需要时使用池,但不要忘记在不再需要时将其关闭。
详细了解各种 Executor
实施及其功能,以便能够以最佳方式找到最符合您要求的实施。
在循环中创建和使用线程的正确方法是什么?
如果我想处理一百万个项目,我将创建一个循环来执行此操作。因此,为了效率起见,我将使其成为多线程并将每个项目分配给一个线程。假设我创建了五个线程来处理这个问题。如何将工作分配给线程?
如果一个线程有一个较小的项目要处理,那么它可以自由处理另一个 - 我如何才能将循环中的下一个项目分配给该线程?
我是否在循环外创建线程然后在循环内使用它们?
这是我正在处理的一个示例 – 它缺少要使用的对象的创建并且只使用了两个线程,但我认为这里的人足够聪明,知道我的意思:)
public class App
{
public static void main( String[] args )
{
App a = new App();
a.doTest();
}
private void doTest() {
Count c = new Count(21);
Count c2 = new Count(7);
Thread t = new Thread(c);
Thread t2 = new Thread(c2);
t.start();
t2.start();
for (int f = 0; f < 10; f++) {
//here - how do I select which thread to send the work to?
// ?????.processThis(nextObject); //how do I send this to the right thread (one that is idle or has least work?)
}
}
public class Count implements Runnable {
private int count;
public void processThis(Object someItemToProcess) {
//here I'll set the object to process and call the method to process it
}
public Count(int count) {
this.count = count;
}
@Override
public void run() {
//for illustration purposes
// for (int i = 1; i < count; i++) {
// System.out.println("Thread " + Thread.currentThread().getId() + " Count = " + i);
// }
}
}
}
此类问题通常通过线程池来解决,您可以将任务提供给线程池并等待完成。在Java中,有几种线程池的实现,你可以从一个固定大小的开始学习:
// Create a thread pool
ExecutorService executor = Executors.newFixedThreadPool(threadsCount);
// Submit your tasks to the pool:
for (Count c: myCounts) {
executor.submit(c);
}
// Shutdown the pool when you don't need it
executor.shutdown();
池将负责并行执行您的任务。如果您需要控制任务的执行、等待它们完成、取消它们、获取它们的工作结果等,只需使用 submit()
方法返回的 Future
对象。您可以在需要时使用池,但不要忘记在不再需要时将其关闭。
详细了解各种 Executor
实施及其功能,以便能够以最佳方式找到最符合您要求的实施。