Created
December 17, 2013 16:28
-
-
Save dgageot/8007801 to your computer and use it in GitHub Desktop.
Thread hell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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