Skip to content

Instantly share code, notes, and snippets.

@dgageot
Created December 17, 2013 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgageot/8007801 to your computer and use it in GitHub Desktop.
Save dgageot/8007801 to your computer and use it in GitHub Desktop.
Thread hell
package net.gageot;
import java.util.*;
import java.util.concurrent.*;
public class TestThreads {
public static final int NB = 100000;
public static void main(String[] args) {
for (Future<Boolean> result : new TestThreads().scheduleTasks()) {
try {
if (!result.get()) {
System.out.println("BUG");
}
} catch (Exception e) {
System.out.println("ERROR");
}
}
System.out.println();
}
private List<Future<Boolean>> scheduleTasks() {
List<Future<Boolean>> results = new ArrayList<Future<Boolean>>();
ExecutorService executor = Executors.newFixedThreadPool(16);
for (final Task task : getTasks()) {
results.add(executor.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
task.run();
return true;
}
}));
}
executor.shutdown();
return results;
}
private static List<Task> getTasks() {
List<Task> tasks = new ArrayList<Task>();
for (int i = 0; i < NB; i++) {
tasks.add(new Task(i));
}
return tasks;
}
public static class Task implements Runnable {
private final int step;
private int[] boulet;
public Task(int step) {
this.step = step;
}
@Override
public void run() {
System.out.println(step);
boulet = new int[6500000];
}
@Override
protected void finalize() throws Throwable {
System.out.println("FINALIZE TASK");
super.finalize();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment