欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一个简易的线程池示例

程序员文章站 2022-07-12 20:38:22
...
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<Runnable>(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);
		}
	}

}



执行结果:

一个简易的线程池示例
            
    
    博客分类: javaThread javathreadpool线程池 
  • 一个简易的线程池示例
            
    
    博客分类: javaThread javathreadpool线程池 
  • 大小: 5.8 KB