---
title: "JDK5.0 Excutor创建线程池"
author: "sole"
date: "2008-08-19T16:11:22Z"
tags: []
slug: "jdk5-0-excutor"
draft: false
lastmod: "2008-08-19T16:11:22Z"
id: 230183
url: "https://hex.iteye.com/blog/230183"
---

```
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolTest {

	public static void main(String[] args) {
		final ExecutorService exec = Executors.newFixedThreadPool(3);
		
		for(int i=1; i<=10; ++i){
			Runnable r = new Task(i);
			exec.execute(r);
		}
		exec.shutdown();
	}

	static class Task implements Runnable {
		private int id;

		Task(int id) {
			this.id = id;
		}

		public void run() {
			try {
				System.out.println(id + " Start");
				Thread.sleep(500);
				System.out.println(id + " Do");
				Thread.sleep(500);
				System.out.println(id + " Exit");
			} catch (Exception e) {
				Thread.currentThread().interrupt();
			}
		}

	}

}
```