mardi 18 janvier 2022

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. When the first object finishes executing, another one starts to execute in it's place. All objects are members of the same class with different input parameters. There can be a lot of objects of this class and therefor I cannot hardcode them as written below. 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();
    

}

Aucun commentaire:

Enregistrer un commentaire