Java 线程中的线程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7224670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Threads within threads in Java?
提问by user253530
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:
我目前正在考虑如何在Java中设计一个需要做一些繁重的网络处理和数据库存储的多线程系统。该程序将首先启动三个基本线程。沿着这些基本线程,我想不是从主程序而是从两个线程启动其他线程。一个线程是否有可能启动另一个导致某种层次结构的线程,例如:
> 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?
如果没有,有人可以提供带参考的理论解决方案吗?
采纳答案by John Smith
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.
是的,您可以根据需要启动任意数量的线程,但这可能不是最好的方法。使用非阻塞 API 会好得多,这样您就可以开始执行某些外部调用,并且调用线程可以立即开始执行其他操作,而无需等待套接字/数据库调用返回。然后,当套接字/数据库调用返回时,将触发回调以完成该处理。
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.
非阻塞 I/O 可以提供更高的 CPU 利用率,因为您只是触发调用和注册回调,而不必尝试平衡“正确”数量的并发线程,而这些线程无论如何都只是在休眠。
http://www.owlmountain.com/tutorials/NonBlockingIo.htm
回答by Jord Sonneveld
To answer the question, yes threads can launch other threads.
要回答这个问题,是的线程可以启动其他线程。
Is the hierarchy important?
等级制度重要吗?
You're probably better off using an ExecutorServicewith a cached thread pool. That way you can pool threads instead of creating lots (which is expensive). ExecutorServices also provide other cool things, and using Callables / Runnables with them is probably much easier to test than mucking about with threads on your own.
您最好将ExecutorService与缓存的线程池一起使用。这样你就可以池线程而不是创建大量(这是昂贵的)。ExecutorServices 还提供了其他很酷的东西,并且与它们一起使用 Callables / Runnables 可能比单独使用线程更容易测试。
回答by JJ.
Yes a thread can launch another thread, and that thread can launch thread(s) and on and on...
是的,一个线程可以启动另一个线程,并且该线程可以启动线程等等......
In the run()
method of a thread - you can create and start other threads.
在run()
线程的方法中 - 您可以创建和启动其他线程。
回答by user1824955
It's possible for exemple you can creat thread and put id in array like this
例如,您可以像这样创建线程并将 id 放入数组中
UnThread[] tab= new UnThread[10] ;
for ( int i=0;i<20;i++)
tab[i] = new UnThread();
After you can give to the subMainThread the array tab
在您可以将数组选项卡提供给 subMainThread 之后
exemple
例子
while( tab[1].isAlive() ) {
//do somthing..
System.out.println("Ligne affichée par le main");
try {
// et faire une pause
tab[1].sleep(800);
}
catch (InterruptedException ex) {}
}
here a simple use of thread : http://kamel.berrayah.com/wordpress/2013/07/java-threads/
这里是线程的简单使用:http: //kamel.berrayah.com/wordpress/2013/07/java-threads/