I have a thread pool with a maximum number of threads of 3. But only one thread is executed, what happened?
public class Volatile {
private volatile static boolean keepA = true;
private static boolean keepB = true;
private static ExecutorService executor = new ThreadPoolExecutor(1,
3, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>() );
public static void main(String[] args) {
executor.execute(() -> {
while (keepA) {
System.out.println("keepA running...");
}
System.out.println("keepA stop!!!!!!");
});
executor.execute(() -> {
while (keepB) {
System.out.println("keepB running...");
}
System.out.println("keepB stop!!!!!!");
});
executor.execute(() -> {
keepA = false;
keepB = false;
});
while (true) {
}
}
}
result
keepA running...
keepA running...
keepA running...
......