一个简易的线程池示例

阅读更多
package thread.pool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池
 * 
 * @author luozhenxing
 * 
 */
public class ThreadPool {
	static ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 200,
			TimeUnit.MILLISECONDS, new ArrayBlockingQueue(5));

	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			ThreadDemo tDemo = new ThreadDemo(i);
			System.out.println("调用" + i);
			executor.execute(tDemo);
		}
//		executor.shutdown();
	}

	static class ThreadDemo implements Runnable {
		int num;

		public ThreadDemo(int num) {
			this.num = num;
		}

		@Override
		public void run() {
			System.out.println("执行线程:" + num);
		}
	}

}



执行结果:

一个简易的线程池示例_第1张图片
  • 一个简易的线程池示例_第2张图片
  • 大小: 5.8 KB
  • 查看图片附件

你可能感兴趣的:(java,thread,pool,线程池)