zoukankan      html  css  js  c++  java
  • 多线程那些事儿

    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++;
                    }
                }
            }
    }
  • 相关阅读:
    BlangenOA项目总结
    ==和Equals与值类型和引用类型
    SQL Server索引
    Html5 之拖动
    Html5 之过渡
    Html 之登录界面
    Html 之进度条
    GUI 之密码框
    GUI 之文本框
    GUI 之列表框
  • 原文地址:https://www.cnblogs.com/shoubianxingchen/p/5555696.html
Copyright © 2011-2022 走看看