Java Concurrent --- Thread & Thread Pool

Thread
Ways to create Threads
  1. extends Thread
  2. implements Runnable
  3. implements Callable and use FutureTask
Thread Pool Knowledge Points
Core Parameter
  • corePoolSize
  • maximumPoolSize
  • keepAliveTime
  • TimeUnit
  • BlockingQueue
  • ThreadFactory
  • RejectedExecutionHandler
Note
  • keepAliveTime used to control non-core Threads’ life time, if we want to control core Threads’ life time, we can use attribute allowCoreThreadTimeOut to set
  • the main blocking queue:
    • ArrayBlockingQueue
    • LinkedBlockingQueue
    • SynchronousQueue: without any capacity, like an empty set
    • PriorityBlockingQueue
  • the main rejectedExecutionHandler:
    • CallerRunsPolicy: only use the thread of the caller to run the task
    • AbortPolicy: default, throws RejectedExecutionException directly
    • DiscardPolicy: discard the task without processing
    • DiscardOldestPolicy: discard the most recent task in the queue and execute the current task
  • the main thread pool:
    • newFixedThreadPool: has a fixed number of threads, only the core thread and there is no timeout mechanism, using LinkedBlockQueue
    • newCachedThreadPool: no core thread, and it’s maxinumPoolSize is MAX_VALUE, that every thread has 60 seconds life time, using SynchronousQueue
    • newScheduledThreadPool: have core threads and the number can be set, that’s maxinum pool size is MAX_VALUE and there is no timeout mechanism, using DelayedWorkQueue
    • newSingleThreadExecutor: one and only one core thread, no timeout mechanism, using LinkedBlockingQueue
Method
  1. excute: no return value, that is impossible to judge whether the execution is successful
  2. submit: will return future, that can judge the execution is successful; If the child thread is not completed, the get method will block until the task is completed, while using the get(long timeout, TimeUnit unit) method will block for a period of time and return immediately. At this time, the task may not be completed
  3. shutdown: set the thread pool state to the SHUTDOWN state, and then interrupt all threads that are not performing tasks
  4. shutdownNow: set the thread pool state to the STOP state, and then interruprt all threads(including those being executed), and return to the list of tasks waiting to be executed
  5. isShutdown: when use shutdown or shutdownNow method, the isShutdown() method returns true; Only when all tasks have been closed, indicating that the thread pool is closed, the isTerminated() method returns true
Work Process
  1. if the number of threads in the thread pool does not reach the number of core threads, then start a core thread to perform the task
  2. if the number of threads in the thread pool has exceeded the number of core threads, the task will be inserted into the task queue and queued for execution
  3. if because the task queue is full, the task cannot be inserted into the task queue. At this time, if the number of threads in the thread pool does not reach the maximum value set by the thread pool, then a non-core thread will be started immediately to perform the task
  4. if the number in the thread pool reaches the specified maximum value, then the task will be rejected, and the rejectedExecution in the RejectedExecutionHandler will be called to notify the caller
Skills
  1. CPU-intensive tasks: due to the faster CPU processing, the number of threads in the thread pool should be as small as possible
  2. IO-intensive tasks: due to the IO operation speed is much lower than the CPU speed, when running such tasks, the CPU is idle most of the time, so the thread pool can be configured with as many threads as possible to improve CPU utilization
  3. Hybrid tasks: it can be divided into CPU-intensive tasks and IO-intensive tasks. When the execution time of these two types of tasks is almost the same, the throughput rate of re-execution through splitting is higher than the throughput rate of serial execution, but if the execution time of these two types of tasks there is a datalevel gap, so there is no meaning to split
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值