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()),
细细想来,也是这个道理:方法跑完的时候方法内所有内部的变量都会被标记为清楚,所以线程池也被标记为清楚了,所以不会占用资源。
时间: 2024-10-13 22:57:20