java多线程

发布于 2024-11-05 19:45:04 字数 509 浏览 0 评论 0原文

我正在开发一个程序,需要根据某些内部到达来生成线程。

  • 我有一个控制类,它产生一个 每个“间隔”都有新线程,这些线程被设置为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮邮递员 2024-11-12 19:45:04

你怎么知道只生成了47个?如果要生成线程,为什么要使用执行器?

  • ExecutorService 有限制吗?我应该换成chached吗?

该限制由内存量和操作系统决定。没有固定的限制。

  • 这是JVM的限制吗?

不,

  • 我怎样才能通过这个限制并产生新线程来保持“到达间隔”?

我不明白这个问题。

  • 有没有最好的方法来实现这一目标?

我不明白你想做什么,所以很遗憾我无法回答。

  • 还有一种方法可以检测 JVM 崩溃之前可以生成的线程数吗?

不可以,因为您可以创建的线程数可能会受到 VM 参数的影响。它还取决于操作系统、内存等。

How do you know that only 47 are spawned? Why are you using an executor if you are spawning threads?

  • is there a limit in the ExecutorService? should I change to chached?

The limit is determined by the amount of memory, and the os. There isn't a fixed limitation.

  • is this a limit of the JVM?

No

  • how can I pass this limit and spawn new threads keeping the "interarrival"?

I don't understand that question.

  • is there a best approach to achieve this?

I haven't understood what you want to do, so I can unfortunately not answer that.

  • also is there a way to detect the number of threads I can spawn before JVM crashes?

No, because the number of threads that you can create can be affected by VM arguments. It will also depend on OS, memory etc.

甜心 2024-11-12 19:45:04

我没有彻底阅读您的代码,但我认为您之前生成的线程有可能已完成运行并死亡,因为每个调用的长度相同并且生成新调用的速率是恒定的(取决于您的个人资料) .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文