说明: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));

         }

    }