控制批量创建新线程
??
說明:Java虛擬機能夠管理的線程數量有限,不加控制的創建新線程可能會導致Java虛擬機崩潰。建議用Java 1.5之后提供的線程池ThreadPoolExecutor來管理線程資源。
?示例:
NG例子:
publicvoid processEntity1(List<Entity> entityList)
{
???????? for(Entity entity : entityList)
{
???????????? newThread(new EntityProcessor(entity)).start();
???????? }
??? }
推薦,由線程池來管理線程資源
private ThreadPoolExecutor threadPool= ...; // init thread pool
?
??? publicvoid processEntity2(List<Entity> entityList)
{
???????? for(Entity entity : entityList)
{
???????????? threadPool.execute(new EntityProcessor(entity));
???????? }
??? }
轉載于:https://blog.51cto.com/joudi/1414712
總結
- 上一篇: 站内优化
- 下一篇: jquery 绑定动态元素