手边星辰

博客园 首页 新随笔 联系 订阅 管理
public void perform(){
        ScheduledExecutorService schedulePool=Executors.newScheduledThreadPool(1);
        
        long initialDelay=10;
        long period=3;
        schedulePool.scheduleAtFixedRate(new Runnable(){
            @Override
            public void run() {
                HttpClient client = new DefaultHttpClient();
                
                String url="http://localhost:8080/member/....../test.do";
                HttpUriRequest get=new HttpGet(url);
                try {
                    HttpResponse res = client.execute(get);
                    System.out.println(EntityUtils.toString(res.getEntity(),"utf-8"));
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally{
                    if(client!=null){
                        client.getConnectionManager().shutdown();  
                    }
                }
                
            }            
        }, initialDelay, period, TimeUnit.SECONDS);
        
    }

监控资源发现,方法内部的线程池关不关闭都无所谓(ExecutorService.shutdown()),

细细想来,也是这个道理:方法跑完的时候方法内所有内部的变量都会被标记为清楚,所以线程池也被标记为清楚了,所以不会占用资源。

  

public void test_Comp(){
            ExecutorService executor=Executors.newCachedThreadPool();
        CompletionService<HashMap<String,Response<B00743RsBody>>> comp=new ExecutorCompletionService<HashMap<String,Response<B00743RsBody>>>(executor);
        
        int count=0;
        for(final String apply:couponNos){
            final EleCoupon ele=map.get(apply);
            if(null==ele){
                log.error("invalid coupon:"+apply);
                continue;
            }
            comp.submit(new Callable<HashMap<String,Response<B00743RsBody>>>(){
                @Override
                public HashMap<String,Response<B00743RsBody>> call() throws Exception {
                    HashMap<String,Response<B00743RsBody>> mmap=new HashMap<String,Response<B00743RsBody>>();
                    Response<B00743RsBody> tcpRes=eleRecycleService.recycle(bankCard, province, ele);
                    mmap.put(apply, tcpRes);
                    return mmap;
                }
            });
            count++;
        }        
        int i=0;
        while(i<count){            
//            poll()是非阻塞的,若目前无结果,返回一个null,线程继续运行不阻塞。
//            take()是阻塞的,若当前无结果,则线程阻塞,直到产生一个结果,被取出返回,线程才继续运行.
            Future<HashMap<String,Response<B00743RsBody>>> ss=comp.poll();            
            if(null!=ss){
                try {
                    HashMap<String,Response<B00743RsBody>> tcpRes=ss.get();
                    String threadReq = tcpRes.keySet().iterator().next();
                    Response<B00743RsBody> threadRes = tcpRes.values().iterator().next();
                    log.info(String.format("the couponNo %s, the resCode %s,the ResMsg %s ",
                            threadReq,threadRes.getResCode(),threadRes.getResMsg()));
                } catch (Exception e) {
                    e.printStackTrace();
                } finally{                    
                    i++;
                }
            }
        }
}

 

posted on 2016-06-03 11:29  手边星辰  阅读(200)  评论(0编辑  收藏  举报