今天决定学习一下java.concurrent包,以这个文章为基础
http://www.yybean.com/examples-of-multi-threaded-framework-java-util-concurrent
1.Executors
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // only two threads ExecutorService exec = Executors.newFixedThreadPool(5); long begin=System.currentTimeMillis(); long begin1=System.currentTimeMillis(); for(int index = 0; index < 100; index++) { long end=System.currentTimeMillis(); System.out.println("cha is "+(end-begin)); begin=end; Runnable run = new Runnable() { public void run() { long time = (long) (Math.random() * 1000); System.out.println("Sleeping " + time + "ms"); try { Thread.sleep(time); } catch (InterruptedException e) { } } }; exec.execute(run); } // must shutdown long end1=System.currentTimeMillis(); System.out.println("GGGGGGG "+(end1-begin1)); exec.shutdown(); } }
输出结果:
cha is 0
cha is 16 cha is 0 cha is 0 cha is 0 Sleeping 805ms Sleeping 782ms Sleeping 715ms Sleeping 110ms cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 Sleeping 66ms cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 cha is 0 GGGGGGG 16 Sleeping 771ms Sleeping 53ms Sleeping 305ms Sleeping 132ms Sleeping 400ms Sleeping 0ms Sleeping 555ms Sleeping 943ms Sleeping 612ms Sleeping 744ms Sleeping 615ms Sleeping 464ms Sleeping 980ms Sleeping 431ms Sleeping 154ms Sleeping 908ms Sleeping 792ms Sleeping 884ms Sleeping 943ms Sleeping 555ms Sleeping 18ms Sleeping 529ms Sleeping 237ms Sleeping 988ms Sleeping 222ms Sleeping 693ms Sleeping 97ms Sleeping 159ms Sleeping 517ms Sleeping 986ms Sleeping 923ms Sleeping 393ms Sleeping 989ms Sleeping 500ms Sleeping 855ms Sleeping 119ms Sleeping 638ms Sleeping 179ms Sleeping 795ms Sleeping 846ms Sleeping 770ms Sleeping 820ms Sleeping 735ms Sleeping 668ms Sleeping 938ms Sleeping 797ms Sleeping 738ms Sleeping 355ms Sleeping 8ms Sleeping 812ms Sleeping 409ms Sleeping 942ms Sleeping 623ms Sleeping 666ms Sleeping 940ms Sleeping 530ms Sleeping 405ms Sleeping 290ms Sleeping 838ms Sleeping 953ms Sleeping 473ms Sleeping 67ms Sleeping 263ms Sleeping 836ms Sleeping 503ms Sleeping 747ms Sleeping 498ms Sleeping 534ms Sleeping 341ms Sleeping 138ms Sleeping 457ms Sleeping 421ms Sleeping 396ms Sleeping 80ms Sleeping 269ms Sleeping 4ms Sleeping 399ms Sleeping 151ms Sleeping 950ms Sleeping 371ms Sleeping 859ms Sleeping 946ms Sleeping 862ms Sleeping 560ms Sleeping 528ms Sleeping 754ms Sleeping 178ms Sleeping 545ms Sleeping 419ms Sleeping 956ms Sleeping 857ms Sleeping 648ms Sleeping 527ms Sleeping 469ms Sleeping 674ms
可见这个 主线程一股脑儿,一下全把这一百个线程放到 excutor的池中,然后两个两个执行。并不会,两个执行完了,再进来两个进行消费。
文章也说了:在for循环的过程中,会等待线程池有空闲的线程,所以主线程会阻塞的。为了解决这个问题,一般启动一个线程来做for循环,就是为了避免由于线程池满了造成主线程阻塞。不过在这里我没有这样处理。[重要修正:经过测试,即使线程池大小小于实际线程数大小,线程池也不会阻塞的,这与Tomcat的线程池不同,它将Runnable实例放到一个“无限”的BlockingQueue中,所以就不用一个线程启动for循环,Doug Lea果然厉害]
经确实,是正确的。
http://www.cnblogs.com/shiyanch/archive/2011/04/04/2005233.html