首页 > 代码库 > 线程池
线程池
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 }
线程池
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。