線程生命週期與線程池

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);
    }
}

}

發佈了163 篇原創文章 · 獲贊 18 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章