简体   繁体   English

如何创建对象列表并一一执行?

[英]How to crate a list of objects and execute them one by one?

I need to create a list of objects and execute them one by one in 2-3 threads.我需要创建一个对象列表并在 2-3 个线程中一个一个地执行它们。 When the first object finishes executing, another one starts to execute in it's place.当第一个 object 完成执行时,另一个开始执行。 All objects are members of the same class with different input parameters.所有对象都是具有不同输入参数的相同 class 的成员。 There can be a lot of objects of this class and therefor I cannot hardcode them as written below.这个 class 可能有很多对象,因此我不能像下面这样对它们进行硬编码。 I like to find the best way to solving this issue.我喜欢找到解决这个问题的最佳方法。

// The simple class. He do nothing 
public class SomeClsass implements Runnable{

    run(){
        sum();
    }
    
    // The simple function
    private void sum(){
        
        // Some code. It doesn't meaning 
        int j =0;
        for(int i=0; i<=10000; i++){
            j=i; 
            
        }
    }
}

public static void main(String args[]){
    
    // I don't wont to create objects as here
    SomeClass someClass = new SomeClass();
    SomeClass someClass1 = new SomeClass();
    SomeClass someClass2 = new SomeClass();
    SomeClass someClass3 = new SomeClass();
    // ...... more then 1000 objects
    
    
    // When this two objects finishes one by one next object will starts executing 
    someClass.run();
    someClass1.run();
    

}

Use ThreadPoolExecutor .使用ThreadPoolExecutor

ExecutorService executor = Executors.newFixedThreadPool(3);
for (int i = 0; i < 1000; i++) {
  SomeClass sc = ....;
  executor.submit(sc);
}
executor.shutdown();
executor.awaitTermination(100, TimeUnit.HOURS);

I would recommend using Executors.newFixedThreadPool too, but with a try catch statement wrapping it.我也建议使用Executors.newFixedThreadPool ,但使用包装它的 try catch 语句。

ExecutorService service = null;
try {
  service = Executors.newFixedThreadPool(3);

  SomeClass someClass = new SomeClass();

  for (int i = 0; i < 10; i++) {
    service.submit(someClass); // Here you could also use a lambda instead of implementing the Runnable interface (() -> someClass.sum())
  }
finally {
  if (service != null) service.shutdown();
}

The example above shows how to use multiple threads to execute your piece of code concurrently, but it is not thread-safe.上面的例子展示了如何使用多个线程同时执行你的一段代码,但它不是线程安全的。 If you want to have multiple threads executing one by one your sum() method, you could use synchronize in your sum() method signature or inside the try catch block ( synchronize(someClass) { for... } )如果你想让多个线程一个一个地执行你的 sum() 方法,你可以在 sum() 方法签名或 try catch 块中使用 synchronize( synchronize(someClass) { for... } )

There are other features in the Java Concurrency API that can be used for this case. Java 并发 API 中还有其他功能可用于这种情况。 I recommend that you look them up before choosing one, because there are other options that are thread-safe, for example atomic classes, synchronized blocks, the Lock framework and cyclic barriers.我建议您在选择之前先查看它们,因为还有其他线程安全的选项,例如原子类、同步块、锁框架和循环障碍。 But the example above is completely usable.但是上面的例子是完全可用的。

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

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