package ch;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
public class PoolTest {
public static void main(String[] args) {
ExecutorService pool=Executors.newFixedThreadPool(10);//申请10个线程池
for(int i=0;i<100;i++) {//100个线程
PoolThread pt=new PoolThread(""+i);
pool.submit(pt);
}
pool.shutdown();
}
}
class PoolThread implements Runnable{
String sName;
public PoolThread(String name) {
// TODO Auto-generated constructor stub
sName=name;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<50;i++) {//每个线程跑50次
System.out.println(sName+"OUT:"+i);
}
}
}
多线程并发实现如图:
说明:每个进程都要循环50次,线程池里多线程并发不会丢失之前已经进行的次数。