81,092
社区成员
发帖
与我相关
我的任务
分享
ExecutorService service= ExecutorService.newCachedThreadPool();
//初始化工5条线程
final Semaphore semp = new Semaphore(5);
for(int index = 0;index < fileNames.size();index++)
{
final String fileName = fileNames.get(i);
Runnable runnable= new Runnable(){
public void run(){
try{
semp.acquire(); //获得许可
execTask(fileName); //执行任务
Thread.sleep((long)(Math.random()*10000));
semp.release(); //访问完后,释放
}catch(Exception ex)(){}
}
};
service.execute(runnable);
}
service.shutdown();