本帖最后由 唐志海 于 2014-1-27 11:41 编辑
这边为什么要用到线程池呢??- public class Road {
- private List<String> vechicles = new ArrayList<String>();
-
- private String name =null;
- public Road(String name){
- this.name = name;
-
- //模拟车辆不断随机上路的过程
- ExecutorService pool = Executors.newSingleThreadExecutor();
- pool.execute(new Runnable(){
- public void run(){
- for(int i=1;i<1000;i++){
- try {
- Thread.sleep((new Random().nextInt(10) + 1) * 1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- vechicles.add(Road.this.name + "_" + i);
- }
- }
-
- });
复制代码
|
|