java无法创建新的线程,java.lang.OutOfMemoryError:无法创建新的本机线程

i saw comment like this

one place i have seen this problem is if you keep creating threads, and instead of calling start(), call run() directly on the thread object.

This will result in the thread object not getting dereferenced...

So after sometime the message unable to create new native thread comes up

In my application, initialy we plan to use thread, but later, we decided no need anymore, so we just call run() instead of start(). Do we need to do manual GC for new threadClass(..) ?

my tomcat startup setting

-Xms1024m -Xmx1024m -XX:MaxPermSize=450m

解决方案

Why do you create a Thread in the first place?

Your code should implement the Runnable interface instead.

Then, when you decide that you want to run it in a thread, simple instantiate a Thread with the Runnable as the argument and call start() on the Thread object.

If, instead, you just want to run it in your current thread, simply call run() on your Runnable object.

This has several advantages:

you don't involve any Thread objects as long as you don't care about separate threads

your code is wrapped in a Runnable which fits closer conceptually: you're not writing some special kind of Thread, do you? You simply write some code that can be executed/run.

you can easily switch to using an Executor which further abstract away the decision

And last but not least you avoid any potential confusion on whether or not a native thread resource is created.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值