ExecutorService: submit(Runnable task) : ExecutorService : java.util.concurrent : Java by API examples (example source code) Organized by topic

Java by API
C++
PHP
Java by API Home »  java.util.concurrent   » [  ExecutorService  ]   
 



ExecutorService: submit(Runnable task)

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class MainClass {
  ExecutorService executor = Executors.newFixedThreadPool(3);

  public void start() throws IOException {
    int i=0;
    while (!executor.isShutdown())
      executor.submit(new MyThread(i++));
  }

  public void shutdown() throws InterruptedException {
    executor.shutdown();
    executor.awaitTermination(30, TimeUnit.SECONDS);
    executor.shutdownNow();
  }

  public static void main(String argv[]) throws Exception {
    new MainClass().start();
  }
}

class MyThread implements Runnable {
private int i;
  MyThread(int i) {
  this.i = i;
  }

  public void run() {
    System.out.println("I am in thread:"+i);
    
  }
}

           
       
Related examples in the same category
1.  ExecutorService: awaitTermination(long timeout, TimeUnit unit)
2.  ExecutorService: shutdown()
3.  ExecutorService: shutdownNow()
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.