newSingleThreadExecutor测试类
package com.thread.pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestSingleThreadExecutor {
public static void main(String[] args) {
ExecutorService executorService = Executors.newSingleThreadExecutor();
for (int i=0; i<10; i++){
Thread thread = new Thread(new ThreadPool("" + i));
executorService.execute(thread);
}
executorService.shutdown();
}
}
线程任务测试类
package com.thread.pool;
import java.util.Date;
public class ThreadPool implements Runnable {
private static ThreadPool threadPool = null;
String name;
public ThreadPool(String name){
this.name = name;
}
@Override
public void run() {
doSomeThing();
}
public static synchronized Runnable getInstance(){
if(threadPool == null){
threadPool = new ThreadPool("getInstance");
}
return getInstance();
}
private void doSomeThing() {
for (int i=0;i<10;i++){
System.out.println((new Date()).toLocaleString() + " " + Thread.currentThread().getName() + "执行" +i + ",name=" + name);
}
}
}