method to call a parallel list?


Charles arrives at Neue:

I have to iterate over the list of each object and call the method, but in parallel. After the loop, there are other declarations, which have to wait for parallel method calls. How can I do this in JAVA?

public void a(List<Object> list) {
    for(Object o : list) {
        asynchMethod(o); // this n method call must run in the same time
    }

    // wait for all asynchMethod result
    /**
     * ...other statements
     */
}

private void asynchMethod(Object o) {
    // some code
}
bramdc:

I see you are using Java 8, then you can use parallelStream's method:

public void a(List<Object> list) {
    list.parallelStream().forEach(s -> asyncMethod(o));
}

It must wait for parallel method calls

The foreach is aka its terminal operation will wait to complete until moving forward to the next line of code: Java Parallel Streams: How to wait for threads to complete in a parallel stream?

If you want some info about parallelStream: https://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html#executing_streams_in_parallel

If you are wondering how many threads parallelstream uses: How many threads are spawned in parallelStream in Java 8?

Be careful with threads and pararellStream, they come with their own heap of problems. Before using them, you should always double check the situation to see if they are worth the trouble they might bring: Should I always use parallel streams if possible?

Related


method to call a parallel list?

Charles arrives at Neue: I have to iterate over the list of each object and call the method, but in parallel. After the loop, there are other declarations, which have to wait for parallel method calls. How can I do this in JAVA? public void a(List<Object> list

method to call a parallel list?

Charles arrives at Neue: I have to iterate over the list of each object and call the method, but in parallel. After the loop, there are other declarations, which have to wait for parallel method calls. How can I do this in JAVA? public void a(List<Object> list

method to call a parallel list?

Charles arrives at Neue: I have to iterate over the list of each object and call the method, but in parallel. After the loop, there are other declarations, which have to wait for parallel method calls. How can I do this in JAVA? public void a(List<Object> list

method to call a parallel list?

Charles arrives at Neue: I have to iterate over the list of each object and call the method, but in parallel. After the loop, there are other declarations, which have to wait for parallel method calls. How can I do this in JAVA? public void a(List<Object> list

method to call a parallel list?

Charles arrives at Neue: I have to iterate over the list of each object and call the method, but in parallel. After the loop, there are other declarations, which have to wait for parallel method calls. How can I do this in JAVA? public void a(List<Object> list

How to call a method of a class in a list

Peppa Pig: I'm making a very basic version of an old Asteroids game with javaFX to start over with programming. When generating asteroids and bullets (objects in a class) I save them in a list. I'm trying to get these asteroids to move around the screen, but I

How to call a method of a class in a list

Peppa Pig: I'm making a very basic version of an old Asteroids game with javaFX to start over with programming. When generating asteroids and bullets (objects in a class) I save them in a list. I'm trying to get these asteroids to move around the screen, but I

Call a method on an object in an array list

Christman I have an ArrayList and I want to call two methods on the first two objects in the list and different methods on the rest of the objects, how can I do this in the easiest way possible? so far i have this ArrayList<Material> materials = new ArrayList(

Call a method on all objects in the list?

Niklas Mandrup Frederickson I have a List playerList with a GoSomething() method on the component PlayerScript. I know I can do: foreach(Player player in playerList) { player.GetComponent<PlayerScript>().DoSomething(); } But this results in a lot of foreach

How to call a method of a class in a list

Peppa Pig: I'm making a very basic version of an old Asteroids game with javaFX to start over with programming. When generating asteroids and bullets (objects in a class) I save them in a list. I'm trying to get these asteroids to move around the screen, but I

Call a method on all objects in the list?

Niklas Mandrup Frederickson I have a List playerList with a GoSomething() method on the component PlayerScript. I know I can do: foreach(Player player in playerList) { player.GetComponent<PlayerScript>().DoSomething(); } But this results in a lot of foreach

Call a method from the Action list

David Klapfner I have this code: public async Task Execute() { _count = 0; _total = 2; //I need to update this every time I add a new code block below. //1st code block _progressCallback("Downloa

call a method and iterate over the list

Lawrence I'm trying to ask for ingredients in a recipe via an input function. I want to store that ingredient in a dictionary, separated by quantity and ingredient. For example, entering "3 eggs" should yield {'3': 'eggs'}. The way I do it is using separate()a

How to call a method of a class in a list

Peppa Pig: I'm making a very basic version of an old Asteroids game with javaFX to start over with programming. When generating asteroids and bullets (objects in a class) I save them in a list. I'm trying to get these asteroids to move around the screen, but I

How to call a method of a class in a list

Peppa Pig: I'm making a very basic version of an old Asteroids game with javaFX to start over with programming. When generating asteroids and bullets (objects in a class) I save them in a list. I'm trying to get these asteroids to move around the screen, but I