简体   繁体   English

是否应该有任何线程驻留在线程池之外?

[英]Should any threads reside outside of the thread pool?

When using a thread pool, is it beneficial to still use singular thread objects for a specific task.使用线程池时,仍然对特定任务使用单个线程对象是否有益。 I'm wondering in terms of a server in Java, whether or not the thread which is listening for connections, should share its resources with any other threads which are then allocated from this one listening thread?我想知道在 Java 中的服务器方面,正在侦听连接的线程是否应该与从这个侦听线程分配的任何其他线程共享其资源? I may also be missing the point as I'm not familiar with this concept.由于我不熟悉这个概念,我也可能错过了这一点。

Yes, singular tasks that have to run concurrently can have their own threads outside of the thread pool.是的,必须同时运行的单个任务可以在线程池之外拥有自己的线程。 Forcing every thread to be part of the pool might obscure your design because you need all kinds of machinery to make concurrent tasks look like worker threads.强制每个线程成为池的一部分可能会混淆您的设计,因为您需要各种机制来使并发任务看起来像工作线程。

I'd create two pools, one for listening and one for internal tasks.我会创建两个池,一个用于监听,一个用于内部任务。 This way you're never putting your server at risk of not being able to listen for connections.这样,您就不会让您的服务器面临无法侦听连接的风险。

The pool for internal tasks can be small if it's only a thread now and then, but at least it's safely isolated.如果内部任务的池不时只是一个线程,那么它可能会很小,但至少它是安全隔离的。

Resource sharing might be necessary in cases where your server needs to maintain a global application state (eg using an AtomicLong for the number of requests served by your server etc.).在您的服务器需要维护全局应用程序 state 的情况下(例如,使用 AtomicLong 来获取服务器所服务的请求数量等),资源共享可能是必要的。 Your main thread would typically wait, ready to accept incoming connections/requests.您的主线程通常会等待,准备好接受传入的连接/请求。 You then update the global state (like hit counter), create a new "job" based on the new request (typically a Runnable or Callable) and submit it to a thread pool (java.util.concurrent) provides them.然后,您更新全局 state(如命中计数器),根据新请求(通常是 Runnable 或 Callable)创建一个新的“作业”并将其提交到提供它们的线程池(java.util.concurrent)。

The purpose of a thread pool is just to help you manage your threads.线程池的目的只是帮助您管理线程。 In other words, a thread pool handles the creation and termination of threads for you as well as giving work to idle threads.换句话说,线程池为您处理线程的创建和终止,并为空闲线程提供工作。 Threads that are blocked or waiting will not receive new tasks.被阻塞或等待的线程将不会接收新任务。

Your connection listener will probably be in an infinite loop waiting for connections and thus never be idle (although it could be in a wait state).您的连接侦听器可能处于无限循环中等待连接,因此永远不会空闲(尽管它可能处于等待状态)。 Since this is the case, the connection listener thread will never be able to receive new tasks so it wouldn't make sense to pool it with the other threads.既然是这种情况,连接侦听器线程将永远无法接收新任务,因此将它与其他线程池化是没有意义的。

Connection listening and connection handling are also two different things.连接监听和连接处理也是两个不同的东西。 From that perspective the connection listener shouldn't be pooled with the connection handlers either.从这个角度来看,连接侦听器也不应该与连接处理程序合并。

SImilar to @larsman's comment, I would do what ever you feel is simpler and clearer.类似于@larsman 的评论,我会做任何你觉得更简单和更清晰的事情。 I have tended to use one thread pool for everything because it appeared to be easier to manage.我倾向于对所有事情都使用一个线程池,因为它似乎更容易管理。 You don't have to do it that way and the listening task can be its own thread.你不必那样做,监听任务可以是它自己的线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM