1. 程式人生 > >執行緒池的使用(Runnable介面)

執行緒池的使用(Runnable介面)

/**
 * 建立執行緒池
 */

public class test03ThreadExecutor {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ExecutorService executorService = Executors.newFixedThreadPool(3);  //建立執行緒池,中有三個執行緒物件
        MyRunnable1 myRunnable = new MyRunnable1();
        executorService.submit(myRunnable);
        executorService.submit(myRunnable);
        executorService.submit(myRunnable);
        executorService.shutdown();

    }

}
class MyRunnable1 implements Runnable{

    @Override
public void run() { // TODO Auto-generated method stub System.out.println("我要一個教練"); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("教練來了"+Thread.currentThread().getName()); System.out.println("教練走了"
); } }