OnJava8-Examples/concurrent/ThreadSize.java

35 lines
827 B
Java
Raw Normal View History

2016-11-23 09:05:26 -08:00
// concurrent/ThreadSize.java
2016-07-05 14:46:09 -06:00
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-07-08 14:42:53 -06:00
// {ValidateByHand}
2016-07-05 14:46:09 -06:00
import java.util.concurrent.*;
public class ThreadSize {
static class Dummy extends Thread {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch(Exception e) {
throw new RuntimeException(e);
}
}
}
public static void main(String[] args) {
ExecutorService exec =
Executors.newCachedThreadPool();
int count = 0;
try {
while(true) {
exec.execute(new Dummy());
count++;
}
} catch(Error e) {
System.out.println(
e.getClass().getSimpleName() + ": " + count);
System.exit(0);
}
}
}