网站开发 图片,网站策划培训,网站建设需要大约多少钱,如何注册网站JavaSE-线程池#xff08;1#xff09;- 线程池概念
前提
使用多线程可以并发处理任务#xff0c;提高程序执行效率。但同时创建和销毁线程会消耗操作系统资源#xff0c;虽然java 使用线程的方式有多种#xff0c;但是在实际使用过程中并不建议使用 new Thread 的方式手…JavaSE-线程池1- 线程池概念
前提
使用多线程可以并发处理任务提高程序执行效率。但同时创建和销毁线程会消耗操作系统资源虽然java 使用线程的方式有多种但是在实际使用过程中并不建议使用 new Thread 的方式手动创建线程。
线程池概念
线程池可以理解成一个存放线程的容器当需要使用线程处理任务时从线程池中取而并非直接创建一个线程
使用线程池的优势
降低资源消耗。通过重复利用已创建的线程降低线程创建和销毁造成的消耗。提高响应速度。当任务到达时任务可以不需要的等到线程创建就能立即执行。提高线程的可管理性。线程是稀缺资源如果无限制的创建不仅会消耗系统资源还会降低系统的稳定性使用线程池可以进行统一的分配调优和监控。
来自 《Java 并发编程的艺术》
线程池相关接口以及类
Runnable
可以理解成一个不需要获取返回结果的任务
FunctionalInterface
public interface Runnable {public abstract void run();
}Callable
类似于 Runnable 是一个有返回结果的任务
FunctionalInterface
public interface CallableV {/*** Computes a result, or throws an exception if unable to do so.** return computed result* throws Exception if unable to compute a result*/V call() throws Exception;
}Future
异步任务提交后使用 Future 接收从 Future get 方法可以获取异步任务的返回值
public interface FutureV {boolean cancel(boolean mayInterruptIfRunning);boolean isCancelled();boolean isDone();V get() throws InterruptedException, ExecutionException;V get(long timeout, TimeUnit unit)throws InterruptedException, ExecutionException, TimeoutException;
}RunnableFuture
Runnable 和 Future 的结合体
public interface RunnableFutureV extends Runnable, FutureV {/*** Sets this Future to the result of its computation* unless it has been cancelled.*/void run();
}FutureTask
RunnableFuture 接口的实现类
public class FutureTaskV implements RunnableFutureV {
}使用demo
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;public class FutureTaskTest {public static void main(String[] args) throws ExecutionException, InterruptedException {FutureTask futureTask new FutureTask(() - {Thread.sleep(1000);return 100;});new Thread(futureTask).start();System.out.println(futureTask.get());}
}Executor
执行器用来执行 Runnable 任务通过实现 Executor 接口可以自定义任务的执行方式比方使用线程池来执行任务避免使用 new Thread 的方式来执行
public interface Executor {/*** 执行方法执行一个具体的 Runnable 任务*/void execute(Runnable command);
}ExecutorService
继承自 Executor 提供更多的方法实现线程池的类一般继承这个接口
public interface ExecutorService extends Executor {/*** 关闭执行器但是会等待已经提交的任务执行完成不再接收新的任务*/void shutdown();/*** 尝试停止所有正在执行的任务不再接收新的任务*/ListRunnable shutdownNow();/***判断执行器是否关闭如果此执行器已关闭则返回true。*/boolean isShutdown();/*** 如果关闭后调用 shutdown 或 shutdownNow 方法所有任务都已完成则返回true。* 请注意除非首先调用shutdown或shutdownNow否则isTerminated 永远不会为true。*/boolean isTerminated();/*** Blocks until all tasks have completed execution after a shutdown* request, or the timeout occurs, or the current thread is* interrupted, whichever happens first.*/boolean awaitTermination(long timeout, TimeUnit unit)throws InterruptedException;/*** 提交一个有返回值的任务并使用 Future 接收*/T FutureT submit(CallableT task);/*** Submits a Runnable task for execution and returns a Future* representing that task. The Futures {code get} method will* return the given result upon successful completion.*/T FutureT submit(Runnable task, T result);/*** 提交任务并使用 Future 接收*/Future? submit(Runnable task);/*** Executes the given tasks, returning a list of Futures holding* their status and results when all complete.* {link Future#isDone} is {code true} for each* element of the returned list.* Note that a emcompleted/em task could have* terminated either normally or by throwing an exception.* The results of this method are undefined if the given* collection is modified while this operation is in progress.*/T ListFutureT invokeAll(Collection? extends CallableT tasks)throws InterruptedException;/*** Executes the given tasks, returning a list of Futures holding* their status and results* when all complete or the timeout expires, whichever happens first.* {link Future#isDone} is {code true} for each* element of the returned list.* Upon return, tasks that have not completed are cancelled.* Note that a emcompleted/em task could have* terminated either normally or by throwing an exception.* The results of this method are undefined if the given* collection is modified while this operation is in progress.*/T ListFutureT invokeAll(Collection? extends CallableT tasks,long timeout, TimeUnit unit)throws InterruptedException;/*** Executes the given tasks, returning the result* of one that has completed successfully (i.e., without throwing* an exception), if any do. Upon normal or exceptional return,* tasks that have not completed are cancelled.* The results of this method are undefined if the given* collection is modified while this operation is in progress.*/T T invokeAny(Collection? extends CallableT tasks)throws InterruptedException, ExecutionException;/*** Executes the given tasks, returning the result* of one that has completed successfully (i.e., without throwing* an exception), if any do before the given timeout elapses.* Upon normal or exceptional return, tasks that have not* completed are cancelled.* The results of this method are undefined if the given* collection is modified while this operation is in progress.*T T invokeAny(Collection? extends CallableT tasks,long timeout, TimeUnit unit)throws InterruptedException, ExecutionException, TimeoutException;
}AbstractExecutorService
public abstract class AbstractExecutorService implements ExecutorService {
}实现 ExecutorService 接口提供ExecutorService执行方法的默认实现
ThreadPoolExecutor
public class ThreadPoolExecutor extends AbstractExecutorService {
}线程池的具体实现类继承自 AbstractExecutorService
类结构图
ThreadPoolExecutor 使用方法
可以使用工具类 Executors 提供的方法创建线程池比如
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ExecutorServiceTest1 {static class MyTask implements Runnable {private int i;public MyTask(int i) {this.i i;}Overridepublic void run() {try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread() 任务 i);}}public static void main(String[] args) {ExecutorService executorService Executors.newFixedThreadPool(2);for (int i 1; i 10; i) {executorService.execute(new MyTask(i));}executorService.shutdown();}
}以上 Executors.newFixedThreadPool 方法创建了一个拥有固定线程数的线城池
public static ExecutorService newFixedThreadPool(int nThreads) {return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueueRunnable());
}执行结果
Thread[pool-1-thread-1,5,main] 任务1
Thread[pool-1-thread-2,5,main] 任务2
Thread[pool-1-thread-2,5,main] 任务4
Thread[pool-1-thread-1,5,main] 任务3
Thread[pool-1-thread-1,5,main] 任务6
Thread[pool-1-thread-2,5,main] 任务5
Thread[pool-1-thread-2,5,main] 任务8
Thread[pool-1-thread-1,5,main] 任务7
Thread[pool-1-thread-2,5,main] 任务9
Thread[pool-1-thread-1,5,main] 任务10通过结果可以看出10个任务都是由两个线程执行的由于这两个线程一次只能处理两个任务其他任务只有在线程空闲时才能被处理实际上线程池不仅维护了一组线程的引用还维护了这组任务而任务则是放在队列中即上文的 LinkedBlockingQueue 参数
参考 https://blog.csdn.net/qq_36881887/article/details/125707550 https://www.mianshigee.com/note/detail/20134hnk/ 文章转载自: http://www.morning.knczz.cn.gov.cn.knczz.cn http://www.morning.ywpwq.cn.gov.cn.ywpwq.cn http://www.morning.nyfyq.cn.gov.cn.nyfyq.cn http://www.morning.gwdnl.cn.gov.cn.gwdnl.cn http://www.morning.dxhdn.cn.gov.cn.dxhdn.cn http://www.morning.rgpy.cn.gov.cn.rgpy.cn http://www.morning.yxwnn.cn.gov.cn.yxwnn.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.nqxdg.cn.gov.cn.nqxdg.cn http://www.morning.wbrf.cn.gov.cn.wbrf.cn http://www.morning.rpzth.cn.gov.cn.rpzth.cn http://www.morning.mttqp.cn.gov.cn.mttqp.cn http://www.morning.kczkq.cn.gov.cn.kczkq.cn http://www.morning.jwefry.cn.gov.cn.jwefry.cn http://www.morning.qgjgsds.com.cn.gov.cn.qgjgsds.com.cn http://www.morning.rlnm.cn.gov.cn.rlnm.cn http://www.morning.nzfqw.cn.gov.cn.nzfqw.cn http://www.morning.yptwn.cn.gov.cn.yptwn.cn http://www.morning.sqlh.cn.gov.cn.sqlh.cn http://www.morning.hcsnk.cn.gov.cn.hcsnk.cn http://www.morning.rwdbz.cn.gov.cn.rwdbz.cn http://www.morning.xrqkm.cn.gov.cn.xrqkm.cn http://www.morning.zqbrw.cn.gov.cn.zqbrw.cn http://www.morning.nqrdx.cn.gov.cn.nqrdx.cn http://www.morning.rfwqt.cn.gov.cn.rfwqt.cn http://www.morning.jbshh.cn.gov.cn.jbshh.cn http://www.morning.dkcpt.cn.gov.cn.dkcpt.cn http://www.morning.cbnlg.cn.gov.cn.cbnlg.cn http://www.morning.qzpkr.cn.gov.cn.qzpkr.cn http://www.morning.kaweilu.com.gov.cn.kaweilu.com http://www.morning.bqwnp.cn.gov.cn.bqwnp.cn http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn http://www.morning.mhwtq.cn.gov.cn.mhwtq.cn http://www.morning.nfbkp.cn.gov.cn.nfbkp.cn http://www.morning.lwzpp.cn.gov.cn.lwzpp.cn http://www.morning.qrndh.cn.gov.cn.qrndh.cn http://www.morning.pxbrg.cn.gov.cn.pxbrg.cn http://www.morning.bkwd.cn.gov.cn.bkwd.cn http://www.morning.mrfr.cn.gov.cn.mrfr.cn http://www.morning.lbgfz.cn.gov.cn.lbgfz.cn http://www.morning.gtbjf.cn.gov.cn.gtbjf.cn http://www.morning.mfxcg.cn.gov.cn.mfxcg.cn http://www.morning.lcqrf.cn.gov.cn.lcqrf.cn http://www.morning.qnzld.cn.gov.cn.qnzld.cn http://www.morning.ydzly.cn.gov.cn.ydzly.cn http://www.morning.wmfny.cn.gov.cn.wmfny.cn http://www.morning.yzzfl.cn.gov.cn.yzzfl.cn http://www.morning.ljxxl.cn.gov.cn.ljxxl.cn http://www.morning.smdkk.cn.gov.cn.smdkk.cn http://www.morning.lcxdm.cn.gov.cn.lcxdm.cn http://www.morning.bfkrf.cn.gov.cn.bfkrf.cn http://www.morning.jfjpn.cn.gov.cn.jfjpn.cn http://www.morning.tslfz.cn.gov.cn.tslfz.cn http://www.morning.qnzk.cn.gov.cn.qnzk.cn http://www.morning.rhdln.cn.gov.cn.rhdln.cn http://www.morning.wbqt.cn.gov.cn.wbqt.cn http://www.morning.smzr.cn.gov.cn.smzr.cn http://www.morning.kdlzz.cn.gov.cn.kdlzz.cn http://www.morning.cytr.cn.gov.cn.cytr.cn http://www.morning.klzdy.cn.gov.cn.klzdy.cn http://www.morning.ybmp.cn.gov.cn.ybmp.cn http://www.morning.xznrk.cn.gov.cn.xznrk.cn http://www.morning.czqqy.cn.gov.cn.czqqy.cn http://www.morning.nqrfd.cn.gov.cn.nqrfd.cn http://www.morning.gynkr.cn.gov.cn.gynkr.cn http://www.morning.qttg.cn.gov.cn.qttg.cn http://www.morning.ynwdk.cn.gov.cn.ynwdk.cn http://www.morning.kmwbq.cn.gov.cn.kmwbq.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.hjwxm.cn.gov.cn.hjwxm.cn http://www.morning.sgnxl.cn.gov.cn.sgnxl.cn http://www.morning.skkmz.cn.gov.cn.skkmz.cn http://www.morning.lxngn.cn.gov.cn.lxngn.cn http://www.morning.llcsd.cn.gov.cn.llcsd.cn http://www.morning.kvzvoew.cn.gov.cn.kvzvoew.cn http://www.morning.fksdd.cn.gov.cn.fksdd.cn http://www.morning.zxqqx.cn.gov.cn.zxqqx.cn http://www.morning.cxlys.cn.gov.cn.cxlys.cn http://www.morning.hkng.cn.gov.cn.hkng.cn http://www.morning.brscd.cn.gov.cn.brscd.cn