parallele-programmierung/u06-5/Concurrent.java

26 lines
525 B
Java

abstract class Concurrent<ArgType> {
private ArgType[] args;
public Concurrent(ArgType[] args) {
this.args = args;
}
void run() {
Thread[] threads = new Thread[args.length];
for (int i = 0; i < args.length; i++) {
final ArgType arg = args[i];
threads[i] = new Thread(() -> perform(arg));
threads[i].start();
}
for (int i = 0; i < threads.length; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
abstract void perform(ArgType arg);
}