This language bar is your friend. Select your favorite languages!

Idiom #56 Launch 1000 parallel tasks and wait for completion

Fork-join : launch the concurrent execution of procedure f with parameter i from 1 to 1000.
Tasks are independent and f(i) doesn't return any value.
Tasks need not run all at the same time, so you may use a pool.
Wait for the completion of the 1000 tasks and then print "Finished".

import java.util.concurrent.*;
class Task implements Runnable {
	int i;
	Task(int i) {
		this.i = i;
	}
	@Override
	public void run() {
		f(i);
	}
}

ExecutorService executor = Executors.newFixedThreadPool(4);
for (int i = 1; i <= 1000; i++) {
	Task task = new Task(i);
	executor.submit(task);
}
executor.awaitTermination(10L, TimeUnit.MINUTES);
System.out.println("Finished");
import std.stdio;
import std.parallelism;
import std.range;
foreach(i; parallel(iota(1,1001))){
	f(i);
}
writeln("Finished");
run_n_times(Fun, N) ->
    Self = self(),
    Task = fun (I) -> spawn(?MODULE, run_task_and_notify_completion, [Self, Fun, I]) end,
    lists:foreach(Task, lists:seq(1, N)),
    wait_for_n_tasks(N).

run_task_and_notify_completion(ParentPid, Fun, Arg) ->
    Fun(Arg),
    ParentPid ! done.

wait_for_n_tasks(0) ->
    io:format("Finished~n");
wait_for_n_tasks(N) when N > 0 ->
    receive
        done ->
            ok
    end,
    wait_for_n_tasks(N-1).

run_n_times(F, 1000).
import "sync"
var wg sync.WaitGroup
wg.Add(1000)
for i := 1; i <= 1000; i++ {
	go func(i int) {
		f(i)
		wg.Done()
	}(i)
}
wg.Wait()
fmt.Println("Finished")
import "async" Control.Concurrent.Async
mapConcurrently f [1..1000]
print "Finished"
use threads;
for my $i (1 .. 1000) {
    threads->create('f', $i);
}
sleep 3 while threads->list(threads::running);
print "Finished\n";

# optional: threads library wants you to explicitly join or detach
# your threads before exiting program.
$_->join() for threads->list(threads::joinable);
from multiprocessing import Pool
def f(i):
	i * i

with Pool(processes) as p:
	p.map(func=f, iterable=range(1, 1001))

print('Finished')
threads = 1000.times.map do |i|
  Thread.new { f(i) }
end
threads.join
use std::thread;
let threads: Vec<_> = (0..1000).map(|i| thread::spawn(move || f(i))).collect();

for t in threads {
	t.join();
}

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

deleplace

Related idioms