说明
有两种方法
方式一:execute()
启动一个线程,没有返回值
例子
package ThreadPool;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class StartThread1 {
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
1,
1,
10,
TimeUnit.SECONDS,
new LinkedBlockingQueue(1)
);
threadPoolExecutor.execute(new MyRun("1"));
threadPoolExecutor.shutdown();
}
static class MyRun implements Runnable {
String name;
public MyRun(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println(name+"--开始");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(name+"--结束");
}
}
}
方式二:submit()
启动一个线程,有返回值
应用场景: 启动线程,执行完毕后,要获取执行结果。