Java中如何重用线程?
我正在构建一个控制台数独求解器,其主要目标是原始速度。
我现在有一个 ManagerThread,它启动 WorkerThreads 来计算每个单元的邻居。因此,现在为每个单元启动一个 WorkerThread。如何重用已完成工作的现有线程?
线程池模式似乎是解决方案,但我不明白如何防止线程在其工作完成后死亡。
ps:我不期望在这个特定任务中获得太多性能,只是想在将多线程应用于代码的更复杂部分之前尝试一下多线程的工作原理。
谢谢
I am a building a console Sudoku Solver where the main objective is raw speed.
I now have a ManagerThread that starts WorkerThreads to compute the neibhbors of each cell. So one WorkerThread is started for each cell right now. How can I re-use an existing thread that has completed its work?
The Thread Pool Pattern seems to be the solution, but I don't understand what to do to prevent the thread from dying once its job has been completed.
ps : I do not expect to gain much performance for this particular task, just want to experiment how multi-threading works before applying it to the more complex parts of the code.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看 Java SE 提供的 <代码>java.util.concurrent API。您可以使用
Executors#newFixedThreadPool()
并且您可以使用ExecutorService
方法。无需重新发明自己的线程池。另请参阅有关该主题的 Sun 教程。Have a look at the Java SE provided
java.util.concurrent
API. You can create a threadpool usingExecutors#newFixedThreadPool()
and you can submit tasks using theExecutorService
methods. No need to reinvent your own threadpool. Also see the Sun tutorial on the subject.当使用线程池(java.util.concurrent)时,您实际上从未初始化过线程 - 而是将 Runnables 传递给线程池。
您无需担心线程生命周期,只需在可运行对象中执行您需要执行的任何工作,并在完成后让它退出。
when using a thread pool (java.util.concurrent) , you never actually initialized a thread - but rather pass Runnables to the thread pool.
you don't need to worry about the thread life-cycle, just do whatever work you need to do in the runnable and let it exit when it's done.
查看使用 CyclicBarrier 同步: http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/CyclicBarrier.html
Have a look into using CyclicBarrier synchro: http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/CyclicBarrier.html
好吧,如果我必须自己编写这个逻辑,而不是使用 OpenSymphony 中的 Quartz 之类的包,我会执行以下操作:
我有一个扩展 Thread 的 WorkerThread。这个类还有一个名为 runnable 的私有属性,即 Runnable。该属性将保存对您要执行的代码的引用。为其设置一个公共设置器。
主线程代码将首先运行您初始化它的可运行对象,然后切换到等待状态。在此之前,它将向池管理器标记它已完成并且可以返回到池中。下次需要线程时,您可以从池中选择一个线程,调用 setRunnable 设置属性 runnable,然后唤醒该线程。它将重新开始工作,进入无限循环:执行并可运行,然后返回等待状态。
Well, if I had to code this logic my self instead of using a package like Quartz from OpenSymphony, I would do the following:
I'd have a WorkerThread which extends Thread. This class will also have private property called runnable which is Runnable. This property will hold a reference to the code you'd like to execute. Have a public setter for it.
The main thread code will start by running the runnable you initialized it with and then switch to a wait state. Before doing that, it will mark to the pool manager that it has finished and it can be returned to the pool. Next time you need a thread, you pick one from the pool, call setRunnable which sets the property runnable, and then wakes up the thread. It will spawn back to work, enter the infinite loop: execute and runnable and go back to wait state.