一个简易的线程池示例
程序员文章站
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); } } }
执行结果: