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,所以会取到后增加的工作。所以能够重用线程继续执行。