你这个可以用线程池解决:
ExecutorService executor = Executors.newFixedThreadPool(10);
Collection<? extends Callable<T>> tasks = new Collection<? extends Callable<T>>();//100个符合格式的任务,自己添加
List<Future> future = executor.invokeAll(tasks);上面任务会在线程池内逐个执行,当10个线程都工作是其他任务等待。当返回的List<Future>集合中的所有Future的isDone()为ture是人物全部执行完毕
ExecutorService executor = Executors.newFixedThreadPool(10);
Collection<? extends Callable<T>> tasks = new Collection<? extends Callable<T>>();//100个符合格式的任务,自己添加
List<Future> future = executor.invokeAll(tasks);上面任务会在线程池内逐个执行,当10个线程都工作是其他任务等待。当返回的List<Future>集合中的所有Future的isDone()为ture是人物全部执行完毕
private static int threadCount = 0; //总线程数
private static int base = 10; //同一时刻最多的线程数
public MakeThread(){
}
public synchronized MakeThread createNewThread(){
base--;
while(base<=0){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
base++;
if(threadCount<100){
threadCount++;
notifyAll();
System.out.println("threadCount:" + threadCount);
return new MakeThread();
}
notifyAll();
return null;
}
public synchronized int getCount(){
return threadCount;
}
public void run(){
//在这里做你想要的事,之后在结束时生成一个新的线程
MakeThread nt = createNewThread();
if(nt!=null)
nt.start();
}
public static void main(String[] args) {
MakeThread old = new MakeThread();
for(int i =0; i<10; i++){
MakeThread mt = old.createNewThread();
if(mt!=null)
mt.start();
}
}
}