package us.google.www;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 線程池
* @author chongrubujing
*
*/
public class ThreadPoolDemo {
public static void main(String[] args) {
//創建一個單線程的線程池
ExecutorService es = Executors.newSingleThreadExecutor();
//創建一個固定大小的線程池
ExecutorService es1 = Executors.newFixedThreadPool(2);
//創建一個可緩存的線程池
ExecutorService es2 = Executors.newCachedThreadPool();
//創建一個無線大小的線程池
ExecutorService es3 = Executors.newScheduledThreadPool(2);
Mythread my = new Mythread();
Mythread my1 = new Mythread();
//執行線程
es3.execute(my);
es3.execute(my1);
}
}
class Mythread implements Runnable
{
@Override
public void run() {
for (int i = 0; i <10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Mythread-"+i);
}
}
}