多线程.线程池
作者:互联网
-
ExecutorService和Executors
-
ExecutorService:真正的线程池接口。常见子类ThreadPoolExecutor
-
void execute(Runnable command):执行任务/命令,没有返回值,一般用来执行Runnable
-
<T>Future<T>submit(Callable<T>tast):执行任务,有返回值,一般用来执行Callable
-
void shutdown():关闭连接池
-
Executors:工具类、线程池的工厂类,用于创建并返回不同类型的线程池
package oop.dxcgaoji; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; //测试线程池 public class TestPool { public static void main(String[] args) { //1.创建服务,创建线程池 //newFixedThreadPool 参数为:线程池大小 ExecutorService service = Executors.newFixedThreadPool(10); service.execute(new MyThread()); service.execute(new MyThread()); service.execute(new MyThread()); service.execute(new MyThread()); //2.关闭链接 service.shutdown(); } } class MyThread implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName()); } }
搜索
复制
标签:execute,service,Executors,void,MyThread,线程,多线程 来源: https://www.cnblogs.com/sususyq-/p/16609380.html