java多线程
我正在开发一个程序,需要根据某些内部到达来生成线程。
- 我有一个控制类,它产生一个 每个“间隔”都有新线程,这些线程被设置为 setDaemon(true)。
- 每个线程都会在经过一段时间后自行停止 “持续时间”时间。 (本地计算 在线程中)。
- 我已经实现了 ExecutorService 与 ExecutorService threadExecutor = Executors.newFixedThreadPool(250);保存 250 个线程的引用,并能够在需要时停止所有线程。
- 这是理论上的,因为经过测试,仅生成 47 个线程。
所以我的问题是:
- 是否有限制 执行者服务?我应该换成chached吗?
- 这是JVM的限制吗?
- 我怎样才能通过这个限制并产生新线程来保持“到达间隔”?
- 有没有最好的方法来实现这一目标?
- 还有一种方法可以检测 JVM 崩溃之前可以生成的线程数吗?
I am developing a program that needs to spawn threads based on some inter arrival.
- I have a control class that spawns a
new thread every "interarrival", those threads are set to setDaemon(true). - Every thread stops itself after a
"duration" time. (calculated locally
in the thread). - I have implemented an ExecutorService with ExecutorService threadExecutor = Executors.newFixedThreadPool(250); to hold a reference of 250 threads and be able to stop all threads if needed.
- This is in theory because after testing, only 47 threads are spawned.
So my questions are:
- is there a limit in the
ExecutorService? should I change to chached? - is this a limit of the JVM?
- how can I pass this limit and spawn new threads keeping the "interarrival"?
- is there a best approach to achieve this?
- also is there a way to detect the number of threads I can spawn before JVM crashes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你怎么知道只生成了47个?如果要生成线程,为什么要使用执行器?
该限制由内存量和操作系统决定。没有固定的限制。
不,
我不明白这个问题。
我不明白你想做什么,所以很遗憾我无法回答。
不可以,因为您可以创建的线程数可能会受到 VM 参数的影响。它还取决于操作系统、内存等。
How do you know that only 47 are spawned? Why are you using an executor if you are spawning threads?
The limit is determined by the amount of memory, and the os. There isn't a fixed limitation.
No
I don't understand that question.
I haven't understood what you want to do, so I can unfortunately not answer that.
No, because the number of threads that you can create can be affected by VM arguments. It will also depend on OS, memory etc.
我没有彻底阅读您的代码,但我认为您之前生成的线程有可能已完成运行并死亡,因为每个调用的长度相同并且生成新调用的速率是恒定的(取决于您的个人资料) .getCallInterarrival();)。当你的程序第一次启动时,它会不断地产生新的线程,直到1分钟后(60000个),调用开始死亡,系统将达到一个平衡,即每创建一个新调用,就有一个调用死亡。
如果我的理论是正确的,您的 profile.getCallInterarrival() 将返回类似 1.33 的值。
I didn't thoroughly read through your code, though i think there is a possibility which the thread you spawned previously has finished running and died, as every call is the same length and the rate of generating new call is constant (depends on your profile.getCallInterarrival();). When your program first start, it would constantly generate new threads until 1 minute later (60000), calls starts to die, and the system will reach an equilibrium which every new call created, there is an call died.
If my theory is right, your profile.getCallInterarrival() will return something like 1.33.