Search code examples
javamultithreadingconcurrencygarbage-collectionhyperthreading

multiple parallel threads in java dont terminate


i try to run a set of parallel thread in java. im creating these through a high order function as follows:

public static void parallelizedMap(Consumer<String> f, List<String> list, int count) {
    List<List<String>> parts = new ArrayList<List<String>>();
    final int N = list.size();
    int L = N / (count - 1);
    for (int i = 0; i < N; i += L) {
        parts.add(new ArrayList<String>(list.subList(i, Math.min(N, i + L))));
    }
    for (List<String> e : parts) {
        Runnable r = new Runnable() {
            public void run() {
                e.forEach(f);
            }
        };
        new Thread(r).start();
    }
}

this method is called every few minutes. it creates hundreds of thread after several minutes. every thread only runs for 20 seconds. but my debugging showed that they never terminate and therefor i get this Exception:

java.lang.OutOfMemoryError: GC overhead limit exceeded

thanks in advance


Solution

  • for me the solution was to give every thread an extra jdbc connection and dont let them share one. this resolved the deadlock und they terminate as expected.