问题描述
我目前正在考虑如何在Java中设计一个需要进行大量网络处理和数据库存储的多线程系统。该程序将首先启动三个基本线程。沿着这些基本线程,我想启动其他线程不是来自主程序,而是来自两个线程。是否有可能线程启动另一个线程,导致某种层次结构,如:
I am currently thinking about how to design a multithreading system in Java that needs to do some heavy network processing and database storage. The program will launch three basic threads at first. Along these basic threads, I would like to launch other threads not from the main program but from two of the threads. Is it possible for a thread to launch another thread leading to some sort of a hierarchy like:
> Parent ->t0 thread1 -> t1 tread1.1
> ->t0 thread2
> ->t0 thread3 -> t2 thread3.1
t0= inital time
t1,t2 = time at a point in the running thread
t1 != t2
如果没有人能提供带参考的理论解决方案?
If not could somebody provide a theoretical solution with references?
推荐答案
是的,您可以根据需要启动任意数量的线程,但这可能不是最好的方法。使用非阻塞API会更好,这样你就可以开始执行一些外部调用,并且调用线程可以立即开始执行其他操作而无需等待socket / database调用返回。然后,当套接字/数据库调用返回时,将触发回调以完成该处理。
Yes, you can launch as many threads as you want, but that's probably not the best way to go. It's much better to use the non-blocking API's so that you can start execution of some external call and the calling thread can immediately start doing something else without waiting on the socket/database call to come back. Then, when the socket/database call comes back, a callback is triggered to finish that processing.
非阻塞I / O可提供更高的CPU利用率,因为您'重新触发调用并注册回调,而不必尝试平衡正确的并发线程数,而这些线程大多只是睡觉。
Non-blocking I/O can provide far superior CPU utilization since you're just triggering calls and registering callbacks and not having to try to balance the "right" number of concurrent threads which are mostly just sleeping anyways.
这篇关于Java中的线程内的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!