线程池newFixedThreadPool的使用

线程池newFixedThreadPool的使用,第1张

线程池newFixedThreadPool的使用,第2张

Examda提示:一个新线程加入后,如果正在运行的线程达到上限,就会阻塞,直到有空个空闲线程可以运行。
导入Java . util . random;
导入Java . util . concurrent . executorservice;
导入Java . util . concurrent . executors;
导入Java . util . concurrent . time unit;
/**
*线程池newFixedThreadPool的使用
*
*
*
*/
public class executor test {
public static void main(String args[]){
Random Random = new Random();
//建立一个容量为3的固定大小线程池
executorservice executor = executors . newfixedthreadpool(3);
//判断线程池可以结束
int wait time = 500;
for(int I = 0;i < 10++){
string name = " thread "+I;
int time = random . nextint(1000);
wait time+= time;
Runnable runner = new executor thread(name,time);
System.out.println("新增:"+name+"/"+time);
executor . execute(runner);
}
尝试{
thread . sleep(wait time);
executor . shut down();
executor . await termination(wait time,TimeUnit。毫秒);
} catch(interrupted exception忽略){
}
}
}
class executor thread实现Runnable {
private final String name;
私有最终int延迟;
public executor thread(String name,int delay){
this . name = name;
this . delay = delay;
}
public void run(){
system . out . println(" startup:"+name);
试试{
Thread.sleep(延时);
} catch(忽略中断异常){
}
system . out . println(" completed:"+name);
}
}

位律师回复
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » 线程池newFixedThreadPool的使用

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情