Note that there are some explanatory texts on larger screens.

plurals
  1. POExecutorService takes too much memory
    text
    copied!<p>Here is the situation: I have to use Xfire to exchange data with a server.The server can not handle to much concurrency.50 is the limit.So I plan to use ExecutorService to limit the amount of concurrent threads.Then Q&amp;A find the usage of memory is nearly 100% when it has 50 concurrency after the program is running 20 mins.</p> <p>Here is my code:</p> <p>public class CompletionServiceImpl {</p> <pre><code>private static Logger logger = Logger.getLogger("BackgroundLog"); private int threadNum; private ExecutorService executor = null; private CompletionService&lt;Integer&gt; sc = null; private static CompletionServiceImpl completionServiceImpl = null; private CompletionServiceImpl(){ this.threadNum = getThreadNum(); this.executor = Executors.newFixedThreadPool(threadNum); this.sc = new ExecutorCompletionService&lt;Integer&gt;(executor); } /*** * get the size of thread pool ***/ private int getThreadNum(){ int threadNum = 5; Properties props = new Properties(); try { props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("tlWeixinconfig.properties")); threadNum = Integer.parseInt(props.getProperty("THREAD_NUM")); } catch (IOException e) { logger.error(e.getMessage(), e); } return threadNum; } public static CompletionServiceImpl getInstance(){ if(completionServiceImpl == null){ synchronized(CompletionServiceImpl.class){ if(completionServiceImpl == null){ logger.info("thread pool is initialized."); completionServiceImpl = new CompletionServiceImpl(); } } } return completionServiceImpl; } public ExecutorService getExecutor() { return executor; } public CompletionService&lt;Integer&gt; getSc() { return sc; } </code></pre> <p>}</p> <hr> <p>public class MyCallable implements Callable{</p> <pre><code>private static Logger logger = Logger.getLogger("BackgroundLog"); private String id; private String usr; private String type; private String expireDate; private String billingURL; private int timeout; private int result; public MyCallable(String id, String usr,String type, String expireDate, String billingURL,int timeout,int result){ super(); this.id = id; this.usr = usr; this.type = type; this.expireDate = expireDate; this.billingURL = billingURL; this.timeout = timeout; this.result = result; } private int newinsertdrawcn(int result)throws Throwable { try { URL url = new URL(billingURL); HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection(); httpConnection.setConnectTimeout(timeout); httpConnection.connect(); Client client = new Client(httpConnection.getInputStream(), null); client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, String.valueOf(timeout)); client.setProperty(CommonsHttpMessageSender.DISABLE_KEEP_ALIVE, "true"); client.setProperty(CommonsHttpMessageSender.DISABLE_EXPECT_CONTINUE, "true"); Object[] results = client.invoke("drawcn", new Object[] {id, usr, type, expireDate }); if (results.length &gt; 0) { result = Integer.parseInt(results[0].toString()); } } catch (Throwable t) { throw t; } return result; } @Override public Integer call(){ try{ result = newinsertdrawcn(result); }catch(Throwable t){ logger.error(t.getMessage(),t); } return result; } </code></pre> <p>}</p> <p>Can anybody explain why and how to solve this problem?</p> <p>or is there someone knows how to limit the amount of concurrent threads?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload