Java intern will be the thread pool knowledge

1. thread pool to know? What are thread pool?

(He answered my question what are the thread pool, to ponder in fact, you should start to answer all aspects such as:

Advantage of the thread pool are:

  1. Reduce resource consumption: reduce consumption of thread creation and destruction caused by repeated use of the thread has been created.
  2. Improve the response speed: When the task arrives, the task may not need to wait for thread creation can be implemented immediately.
  3. Improve the manageability thread: using a thread pool thread can be unified distribution, scheduling and monitoring.

java thread pool created four ways:

  1. newCachedThreadPool create a cached thread pool, thread pool longer than if treatment needs, the flexibility to reclaim idle thread, if not recyclable, the new thread
  2. newFixedThreadPool create a fixed-size thread pool, you can control the maximum number of concurrent threads, excess threads will wait in the queue
  3. newScheduledThreadPool create a fixed-size thread pool to support regular and periodic task execution
  4. newSingleThreadExecutor create a single-threaded thread pool, it will only work threads to perform tasks to ensure that all tasks as specified.

Parameters and methods and strategies thread pool:
important parameters: (7)

  • CorePoolSize: a core number of threads
  • MaximumPoolSize: maximum number of threads
  • keepAliveTime when the number of thread pool threads is greater than corePoolSize time, the extra idle threads, how long it will be destroyed.
  • TimeUnit: Retention time in idle thread (* Detailed point can be said not to say)
    • TimeUnit.DAYS; // day
    • TimeUnit.HOURS; // hours
    • TimeUnit.MINUTES; // minutes
    • TimeUnit.SECONDS; // seconds
    • TimeUnit.MILLISECONDS; // millisecond
    • TimeUnit.MICROSECONDS; // subtle
    • TimeUnit.NANOSECONDS; // ns
  • WorkQueue: task buffer queue for storing task submitted but has not yet been executed
  • ThreadFactory: a thread factory used to create the main thread
  • Handler: saturation processing strategy (denial policy)
    saturation processing strategy will typically ask you how much you know Handler (so that his ideas would go down your ideas, you have the initiative)??
    Saturation processing strategies:
  1. ThreadPoolExecutor.AbortPolicy: RejectedExecutionException discard task and throw an exception, JDK defaults to this policy.
  2. ThreadPoolExecutor.DiscardPolicy: no treatment, drop new tasks, but do not throw an exception.
  3. ThreadPoolExecutor.DiscardOldestPolicy: recently dropped from the queue of tasks, and then try to perform the task (repeat this process)
  4. ThreadPoolExecutor.CallerRunsPolicy: mission after waiting caller thread is idle. The caller thread: Suppose create a thread pool thread A, thread A is the caller's thread

2. Thread execution flow into the thread pool? (That is, the working principle of the thread pool

Thread State

  1. New
    new statement creates a thread object in the new state, then it and other java objects, only allocated memory.
  2. Wait
    ago when a thread after a new, and calls the start method, the thread is in a wait state.
  3. Ready
    When a thread object is created, other threads calling its start () method, the thread into the ready state. Thread in this state in the Java virtual machine can run the pool, waiting for the right to use the cpu.
  4. Operating status
    thread is in this state of occupation of CPU, executes the program code. In a concurrent runtime environment, if the computer has only one CPU, so any time there is only one thread is in this state. Threads have a chance only in a ready state to run state.
  5. Blocking state
    blocking state means that thread for some reason to give up CPU, temporarily stop running. When a thread is blocked, Java Virtual Machine CPU will not be assigned to a thread, until the thread re-enter the ready state, it will have access to running.

Blocking state is divided into three types:
(1) wait for blocking: execution wait thread run () method, JVM will thread into the wait pool.
(2), synchronous blocking: threads running at the time of acquiring the object synchronization lock, if the synchronization lock is occupied by another thread, the JVM thread will lock into the pool.
(3), the blocked: execution threads running Sleep () method, or to issue the I / O request, the JVM will thread to a blocked state. When the Sleep () timeout, or I / O processing is completed, the thread into the ready state again.

  1. Death state
    when threaded code method of executing the run (), or encounter uncaught exception, will exit run () method, then they enter the death
    state, the thread end of the life cycle.

Run process:
first determine whether the core thread pool threads are on a mission, if not then create a thread pool thread directly from the core to execute, if you are busy judging whether the task queue is full, then the task is not full into them awaiting execution, full on whether all the threads in the thread pool are busy, if you are busy on to saturation strategy to deal with, if not, create a thread to help the core thread processing tasks.
* Figure ()Here Insert Picture Description
https://blog.csdn.net/lchq1995/article/details/85230399 (specifically to see this blog)

Published 12 original articles · won praise 2 · Views 422

Guess you like

Origin blog.csdn.net/weixin_43308406/article/details/102990587