Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ThreadPoolFiddle {
- public static void main (String[] args) throws InterruptedException {
- ExecutorService executorService = Executors.newFixedThreadPool(2);
- for(int i=0; i<10; i++) {
- executorService.submit(new MyRunnable(i));
- }
- executorService.shutdown();
- executorService.awaitTermination(1, TimeUnit.DAYS);
- }
- }
- class MyRunnable implements Runnable {
- private int id;
- public MyRunnable(int id) {
- this.id = id;
- }
- @Override
- public void run() {
- System.out.println("Running " + id);
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(id + " finished");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement