关于java:threadPoolExecutor

37次阅读

共计 922 个字符,预计需要花费 3 分钟才能阅读完成。

1、线程池 ThreadPoolExecutor 的应用,队列的应用:SynchronousQueue not hold task,handoff task to thread,so it will false when dont have thread to procee the task ,or create a new thread to process the task,general, 会设置最大线程无边界一起应用,LinkedBlockingQueue,Unbouded queue,the maxmumpoolSize is not effect,for example in a web page is useful in smooth out transient bursts of request.ArrayBlockingQueue,bouded queue.
2、how to judge a thread is idle?
线程在 getTask 时候,判断外围线程是否能够超时或者线程数是否大于外围线程,上一步如果为 true 示意须要有可能须要淘汰线程,而后带超时参数区获取 task,如果超时后仍未获取道工作,则能够判断线程是闲暇的,须要缩小工作线程数并进行该线程。
3、submit 和 execute 用哪个办法来执行工作。都能够,submit return future ,you can get a reslut.execute just perform.
4、线程池 worker 线程工作原理:
首先 execute(runnable), 增加第一个工作,此时线程数少于外围线程数,构建 worker 线程,worker 自身是一个 runnable, 成员变量其中蕴含一个 firstTask 和一个 thread(this), 构建好了之后,调用 worker.thread.start(),worker.run 会执行,执行过程中,会判断 firstTask,如果存在则执行 firstTask, 否则判断 getTask 存不存在工作,getTask 又会调用阻塞队列,有工作时会执行从队列中取到的工作。
当初来说当外围线程已满时的处理过程,就是会把工作放到阻塞队列,因为之前执行过 firstTask 工作之后的线程会调用 getTask,所以会取到后增加的工作。所以能够重用线程继续执行。

正文完
 0