我们收到“java.lang.OutOfMemory错误:在大约1024个线程后,无法在jboss上创建新的本地线程””,因为应用程序占用了所有最大用户进程
ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 14866
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
**max user processes (-u) 1024**
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
发生这一切的代码如下:
ExecutorService service = Executors.newFixedThreadPool(2);
Collection<Callable<String>> tasks = new ArrayList<Callable<String>>();
tasks.add(ctgService);**--> THIS IS THE LINE OF OOM**
try{
List<Future<String>> taskFutures = service.invokeAll(tasks, 60L, TimeUnit.SECONDS);
for (Future<String> future : taskFutures) {
ctgMsgOut = future.get();
}
} catch (InterruptedException ie){
throw new TimeoutException("The response from GEC-CTG has exceeded the timeout");
} catch (CancellationException ce){
throw new TimeoutException("The response from GEC-CTG has exceeded the timeout");
} catch (ExecutionException ee){
if (ee.getCause() instanceof TimeoutException){
throw new TimeoutException("The response from GEC-CTG has exceeded the timeout");
}else{
throw new ConnectException("Connect exception");
}
} catch (Exception e){
e.printStackTrace();
}finally {
service.shutdown();
}
ctgMsgOut = "<MessageOutGEC>" + ctgMsgOut + "</MessageOutGEC>";
exchange.getOut().setBody(ctgMsgOut, String.class);
exchange.getOut().setHeaders(exchange.getIn().getHeaders());
请你能帮我弄明白代码哪里不对吗?在service.shutdown()之后我应该添加tasks.clear()还是tasks.remove(ctgService)?
谢谢你
1条答案
按热度按时间bbuxkriu1#
如果在rest方法中调用此代码,并且该rest方法被并行调用600次,则会同时创建1200个线程。
让所有的方法调用都使用相同的
ExecutorService service = Executors.newFixedThreadPool(2);
--或者如果你在现代的jboss/wildfly中,最好注入一个ManagedExecutorService
。