我有一个部署在 tomcat 6
中的应用程序
在这个应用程序中,我创建了一个线程类
,它从一个表中获取 1000 条记录并处理它们,并将它们存储在 DataBase
中的 4 个不同的表中。
我同时运行两个线程
以防止进程发生死锁
。
有什么方法可以根据我的cpu 利用率
安排这些线程
。
IE。如果没有剩余的 cpu,则不会创建新线程。
最佳答案
如果我正确理解了您的问题,我认为 Joshua Bloch 的“Effective Java”第 2 版中考虑了这种情况:
If a server is so heavily loaded that all of its CPUs are fully utilized, and more tasks arrive, more threads will be created, which will only make matters worse. Therefore, in a heavily loaded production server, you are much better off using Executors.newFixedThreadPool, which gives you a pool with a fixed number of threads, or using the ThreadPoolExecutor class directly, for maximum control.
虽然我还没有尝试过,但使用 Executors 似乎是个好主意。
关于java多线程cpu利用率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9512024/