One. Thread state Type 1. New state: A new Thread object was created. 2. Ready state (Runnable): After the thread object is created, other threads call the object's start () method. The state of the thread is located in a pool of running threads that becomes operational and waits for the CPU to be used. 3. Running state (Running): The ready state of the thread gets the CPU and executes the program code. 4. Blocking state (Blocked): The blocking state is a temporary stop for a thread that has abandoned the CPU usage for some reason. Until the thread is in a ready state, the opportunity to go to the running state is reached. There are three types of blocking: (i), waiting for blocking: The running thread executes the wait () method, and the JVM puts the thread into the waiting pool. (ii), synchronous blocking: When a running thread acquires a synchronization lock on an object, the JVM puts the thread into the lock pool if the synchronization lock is occupied by another thread. (iii), other blocking: The running thread executes the sleep () or join () method, or when an I/O request is made, the JVM will place the thread in a blocked state. When the sleep () state times out, join () waits for the thread to terminate or time out, or the I/O process finishes, the thread is re-entered in a ready state. 5. Death status (Dead): The thread finishes executing or exits the run () method because of an exception, and the thread ends the life cycle.
Members of the two-executor framework
The overall design approach to the above steps is to avoid acquiring a global lock as much as possible when executing the Execute () method (that would be a severe, scalable bottleneck). After Threadpoolexecutor finishes preheating (the number of threads currently running is greater than or equal to corepoolsize), almost all of the Execute () method calls are performed to store the task in the queue, and step 2 does not need to acquire a global lock. 2.1 threadpoolexecutor2.1.1 Create thread new Threadpoolexecutor (Corepoolsize, Maximumpoolsize, KeepAliveTime, Milliseconds,runnabletaskqueue, handler), or use the factory class executors to create. You can also create three kinds of singlethreadexecutor, Fixedthreadpool, and Cachedthreadpool. fixedthreadpool uses the unbounded queue Linkedblockingqueue as the worker queue for the thread pool (the queue has a capacity of integer.max_value). The use of unbounded queues as work queues has the following effect on the thread pool. 1) When the number of threads in the thread pool reaches corepoolsize, the new task waits in the unbounded queue, so the number of threads in the thread pool does not exceed corepoolsize. 2) due to 1, maximumpoolsize will be an invalid parameter when using unbounded queue. 3) due to 1 and 2, KeepAliveTime will be an invalid parameter when using a unbounded queue. 4) due to the use of unbounded queues, the running Fixedthreadpool (no method shutdown () or Shutdownnow () ) does not reject the task (the Rejectedexecutionhandler.rejectedexecution method is not called). Singlethreadexecutor's corepoolsize and Maximumpoolsize are set to 1, using the unbounded queue Linkedblockingqueue as the work queue for the thread pool, The corepoolsize that affect the same cachedthreadpool as Fixedthreadpool is set to 0, that is, Corepool is empty; Maximumpoolsize is set to Integer.max_value, That is, the maximumpool is unbounded. Setting the KeepAliveTime to 60L here means that the idle thread waits for the new task'sThe maximum time is 60 seconds and the idle thread will be terminated after more than 60 seconds. Linkedblockingqueue with unbounded queue. In extreme cases, CPU and memory resources are exhausted because of multiple threads being created. The 2.1.2 Execute thread execute () method is used to commit a task that does not require a return value, so it is not possible to determine whether the task was successfully executed by the thread pool. The Submit () method is used to submit a task that requires a return value. The thread pool returns an object of the future type, through which the future object can determine whether the task is successful, and can get the return value through the Get () method of the future, the Get () method blocks the current thread until the task is completed, and uses the Get (long The Timeout,timeunit unit) method blocks the current thread from returning immediately after a period of time, when it is possible that the task is not completed. Threadspool.execute (New Runnable () {@Overridepublic void Run () {//TODO auto-generated Method stub}}); 2.2scheduledthreadpoolexecutor Scheduledthreadpoolexecutor. Contains several threads that are suitable for scenarios that require multiple background threads to perform periodic tasks, while limiting the number of background threads in order to meet the needs of resource management. 1) when calling Scheduledthreadpoolexecutor's Scheduleatfixedrate () method or the Schedulewith-fixeddelay () method, A scheduledfuturetask that implements the Runnablescheduledfutur interface is added to the Scheduledthreadpoolexecutor delayqueue. 2) threads in the thread pool get scheduledfuturetask from Delayqueue, and then perform the task. Singlethreadscheduledexecutor. Contains only one thread that is suitable for a single background thread to perform periodic tasks while requiring the application farm 3 to ensure sequential execution of each task. To properly configure a thread pool to properly configure threads, you must first analyze the task characteristics, which can be analyzed from several angles. • Nature of tasks: CPU-intensive tasks, IO-intensive tasks and mixed tasks. • Task priority: High, medium, and low. • Task execution time: long, short. • Task dependencies: whether to rely on other system resources, such as database connections. Tasks of different nature can be used in different rulesThe thread pool of the module is processed separately. CPU-intensive tasks should be configured with as small a thread as possible, such as configuring a thread pool of ncpu+1 threads. Because IO-intensive task threads do not always perform tasks, you should configure as many threads as possible, such as 2*NCPU. A mixed-type task, if it can be split, split it into a CPU-intensive task and an IO-intensive task, as long as the time difference between the two tasks is not too large, then the throughput performed after the decomposition will be higher than the throughput of serial execution. If the execution time of these two tasks is too large, no decomposition is necessary. It is recommended to use a bounded queue. I/O bound refers to the system's CPU performance is much better than the hard disk/memory performance, at this time, the system operation, most of the situation is CPU in the I/O (hard disk/memory) read/write, at this time the CPU Loading is not high. CPU bound refers to the system's hard disk/memory performance is much better than the CPU performance, at this time, the system operation, most of the situation is CPU Loading 100%,CPU to read/write I/O (hard disk/memory), I/O can be completed in a short time, and the CPU has a lot of operational CPU Loading very high.
Multithreaded state and thread pool management