1. 先创建自定义ThreadCallBack类,实现Callable
public class ThreadCallBack implements Callable {
private static Callable callable;
public static synchronized Callable getInstance(){
if(callable == null){
callable = new ThreadCallBack();
}
return callable;
}
@Override
public Object call() throws Exception {
for (int i=0;i<3;i++){
System.out.println((new Date()).toLocaleString() + " " + Thread.currentThread().getName() + "执行ing");
}
// Thread.sleep(2000);
return "return true";
}
}
此类中重写call方法,其中返回执行结果 "return true"。
2. 然后新建测试类ThreadPoolTest
public class ThreadPoolTest {
@Test
public void test() throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(5);
fo