=================================================================
TestMain.java
import test.thread.test.DBTest;
import test.thread.test.DoSomething;
import test.thread.test.ListPool;
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
DBTest.initListObject();
int threadCount = 1;
try {
while (threadCount < 1000) {// 必须保留最后一个,做为下一次更新时的起初id
System.out.println("线程个数:" + threadCount);
DoSomething doSomething = new DoSomething();
Thread thread = new Thread(doSomething);
//thread.setDaemon(true);//这里在tomcat或者其它的web应用程序方面启动线程时应该设置为true.在 tomcat关闭的时候,线程也应该关闭.因为用户线程是基于jvm的,如果不设置为true(守护线程),tomcat关闭的时候,线程还会在运行,那会消耗cpu与内存的.
thread.setDaemon(false);//在main方法中,这里必须设置为用户线程.因为设置为守护线程时,main方法执行完毕线程也自动退出了,事实上程序还没有执行完成.
thread.start();
threadCount++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}