加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 23:44:45 所属栏目:Java 来源:网络整理
导读:我看到这样的评论 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
我看到这样的评论

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

在Sun Java Forums

在我的应用程序中,我们最初计划使用线程,但后来我们决定不再需要了,所以我们只调用run()而不是start().我们是否需要为新的threadClass(..)做手动GC?

我的tomcat启动设置

-Xms1024m -Xmx1024m -XX:MaxPermSize=450m

解决方法

为什么要首先创建一个Thread?

您的代码应该实现Runnable接口.

然后,当您决定要在线程中运行它时,使用Runnable作为参数简单地实例化一个Thread,并在Thread对象上调用start().

相反,如果您只想在当前线程中运行它,只需在Runnable对象上调用run()即可.

这有几个好处:

>只要您不关心单独的线程,就不会涉及任何Thread对象
>你的代码被包装在一个Runnable中,它在概念上更接近:你不是在写一些特殊的Thread,对吗?您只需编写一些可以执行/运行的代码.
>您可以轻松切换到使用Executor,进一步抽象决定

最后但并非最不重要的是,您避免了对是否创建本机线程资源的任何可能的混淆.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读