21 package org.matsim.core.population.algorithms;
23 import java.lang.Thread.UncaughtExceptionHandler;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.concurrent.atomic.AtomicBoolean;
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
76 int numOfThreads = Math.max(numberOfThreads, 1);
78 Thread[] threads =
new Thread[numOfThreads];
82 final AtomicBoolean hadException =
new AtomicBoolean(
false);
86 for (
int i = 0; i < numOfThreads; i++) {
89 name = algo.getClass().getSimpleName();
90 counter =
new Counter(
"[" + name +
"] handled person # ");
93 Thread thread =
new Thread(algothread, name +
"." + i);
94 thread.setUncaughtExceptionHandler(uncaughtExceptionHandler);
96 algoThreads[i] = algothread;
107 for (Thread thread : threads) {
113 for (Thread thread : threads) {
117 }
catch (InterruptedException e) {
120 if (hadException.get()) {
121 throw new RuntimeException(
"Exception while processing persons. Cannot guarantee that all persons have been fully processed.");
131 private final List<Person> persons =
new LinkedList<Person>();
135 this.personAlgo = algo;
136 this.counter = counter;
140 this.persons.add(person);
145 for (
Person person : this.persons) {
146 this.personAlgo.
run(person);
160 this.hadException = hadException;
165 log.error(
"Thread " + t.getName() +
" died with exception while handling events.", e);
166 this.hadException.set(
true);
PersonAlgorithm getPersonAlgorithm()
final PersonAlgorithm personAlgo
void uncaughtException(Thread t, Throwable e)
Map< Id< Person >,? extends Person > getPersons()
void handlePerson(final Person person)
static void run(final Population population, final int numberOfThreads, final PersonAlgorithmProvider algoProvider)
ParallelPersonAlgorithmUtils()
static void run(final Population population, final int numberOfThreads, final PersonAlgorithm algorithm)
PersonAlgoThread(final PersonAlgorithm algo, final Counter counter)
final AtomicBoolean hadException
ExceptionHandler(final AtomicBoolean hadException)