TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Thread Pools on the JVM

217 pointsby ovisalmost 4 years ago

8 comments

cogman10almost 4 years ago
Loom can&#x27;t land fast enough!<p>The current issue the JVM has is that all threads have a corresponding operating system thread. That, unfortunately, is really heavy memory wise and on the OS context switcher.<p>Loom allows java to have threads as light weight as a goroutine. It&#x27;s going to change the way everything works. You might still have a dedicated CPU bound thread pool (the common fork join pool exists and probably should be used for that). But otherwise, you&#x27;ll just spin up virtual threads and do away with all the consternation over how to manage thread pools and what a thread pool should be used for.
评论 #27885738 未加载
评论 #27884955 未加载
评论 #27884719 未加载
评论 #27889201 未加载
评论 #27890016 未加载
评论 #27884609 未加载
评论 #27884392 未加载
评论 #27885369 未加载
jackcviers3almost 4 years ago
Author mentions scala. Both ZIO[1] and Cats-Effect[2] provide fibers (coroutines) over these specific threadpool designs today, without the need for Project Loom, and give the user the capability of selecting the pool type to use without explicit reference. They are unusable from Java, sadly, as the schedulers and ExecutionContexts and runtime are implicitly provided in sealed companion objects and are therefore private and inaccessible to Java code, even when compiling with ScalaThenJava. Basically, you cannot run an IO from Java code.<p>You can expose a method on the scala side to enter the IO world that will take your arguments and run them in the IO environment, returning a result to you, or notifying some Java class using Observer&#x2F;Observable. This can, of course take Java lambdas and datatypes, thus keeping your business code in Java should you so desire. It&#x27;s clunky, though, and I wish Java had easy IO primitives like Scala.<p>1. <a href="https:&#x2F;&#x2F;github.com&#x2F;zio&#x2F;zio" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;zio&#x2F;zio</a><p>2. <a href="https:&#x2F;&#x2F;typelevel.org&#x2F;cats-effect&#x2F;versions" rel="nofollow">https:&#x2F;&#x2F;typelevel.org&#x2F;cats-effect&#x2F;versions</a>
评论 #27885328 未加载
评论 #27887603 未加载
jfoutzalmost 4 years ago
I&#x27;m wary of unbounded thread pools. Production has a funny way of showing that threads always consume resources. A fun example is file descriptors. An unexpected database reboot is often a short outage, but it&#x27;s crazy how quickly unbounded thread pools can amplify errors and delay recovery.<p>Anyway, they have their place, but if you&#x27;ve got a fancy chain of micro services calling out to wherever, think hard before putting those calls in an unbounded thread pool.
评论 #27885350 未加载
评论 #27885412 未加载
charleslmungeralmost 4 years ago
Another tip - If you have a dynamically-sized thread pool, make it use a minimum of two threads. Otherwise developers will get used to guaranteed serialization of tasks, and you&#x27;ll never be able to change it.
评论 #27886526 未加载
评论 #27886656 未加载
0xffff2almost 4 years ago
This seems like good advice in general. Is any of it really specific to the JVM? If I was doing thread pooling with CPU and IO bound tasks, I would approach threading in a similar way in C++.
评论 #27884357 未加载
评论 #27884456 未加载
WatchDogalmost 4 years ago
If your app is fully non-blocking, doesn&#x27;t it make sense to just do everything on the one pool, CPU bound tasks and IO polling. Rather than passing messages between threads.
评论 #27889491 未加载
elricalmost 4 years ago
&gt; you&#x27;re almost always going to have some sort of singleton object somewhere in your application which just has these three pools, pre-configured for use<p>I&#x27;m bemused by this statement, and I can&#x27;t figure out whether this is an assertion rooted in supreme confidence, or just idle, wishful thinking.<p>That being said, giving threading advice in a virtualized and containerized world is tricky. And while these three categories seem sensible, mapping the functions of any non-trivial system onto them is going to be difficult, unless the system was specifically designed around it.
u678ualmost 4 years ago
With Python at first I was scared of GIL being single threaded, now I&#x27;m used to it and it works great. Thousands of threads used to be normal for my old Java projects but seems crazy to me now.