线程池
Posted 云中之歌
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线程池相关的知识,希望对你有一定的参考价值。
1 package threads; 2 3 import java.util.concurrent.ExecutorService; 4 import java.util.concurrent.Executors; 5 6 public class ThreadPoolTest { 7 public static void Fixed() { 8 ExecutorService pool = Executors.newFixedThreadPool(6); 9 Runnable target = new Runnable() { 10 @Override 11 public void run() { 12 // TODO Auto-generated method stub 13 for (int i=0; i<5; i++) { 14 System.out.println(Thread.currentThread().getName()+" 的 i 值为 " + i); 15 } 16 } 17 18 }; 19 pool.submit(target); 20 pool.submit(target); 21 pool.shutdown(); 22 } 23 24 public static void Cached() { 25 ExecutorService pool = Executors.newCachedThreadPool(); 26 Runnable target = new Runnable() { 27 @Override 28 public void run() { 29 for (int i=0; i<5; i++) { 30 System.out.println(Thread.currentThread().getName()+" 的 i 值为 " + i); 31 } 32 } 33 34 }; 35 pool.submit(target); 36 pool.submit(target); 37 pool.shutdown(); 38 } 39 40 public static void main(String[] args) { 41 //ThreadPoolTest.Fixed(); 42 ThreadPoolTest.Cached(); 43 } 44 }
以上是关于线程池的主要内容,如果未能解决你的问题,请参考以下文章