02-Thread--Basic thread mechanism

Basic threading mechanism

Executor

The life cycle of the Executor manager thread provides multiple methods for us to use.


Insert picture description here
The top-level interface implemented by Executor's inheritance system ThreadPoolExecutor is Executor, and the top-level interface Executor provides an idea: decoupling task submission and task execution. Users do not need to pay attention to how to create threads and how to schedule threads to perform tasks. Users only need to provide Runnable objects and submit the running logic of the tasks to the Executor. The Executor framework completes the deployment of threads and the execution of tasks. The ExecutorService interface adds some capabilities: (1) Expanding the ability to execute tasks, supplementing the method that can generate Futures for one or a batch of asynchronous tasks; (2) Providing methods to control the thread pool, such as stopping the running of the thread pool. AbstractExecutorService is an upper-level abstract class that connects the process of executing tasks in series to ensure that the implementation of the lower layer only needs to focus on one method of executing the task. The lowest-level implementation class ThreadPoolExecutor implements the most complex operation part. ThreadPoolExecutor will maintain its own life cycle on the one hand, and manage threads and tasks at the same time, so that the two can be combined to perform parallel tasks.

Retrieved from: https://tech.meituan.com/2020/04/02/java-pooling-pratice-in-meituan.html

Daemo thread

Reading materials: https://blog.csdn.net/shimiso/article/details/8964414

When all non-daemon threads end, the program is terminated, and all daemon threads are killed at the same time.

main() is a non-daemon thread.

Before the thread starts, use the setDaemon() method to set a thread as a daemon thread.

public static void main(String[] args) {
    
    
    Thread thread = new Thread(new MyRunnable());
    thread.setDaemon(true);
}

wait , sleep , yield

阅读素材一: https://javarevisited.blogspot.com/2011/12/difference-between-wait-sleep-yield.html#axzz6n07ehEDk
This difference is more obvious from the fact that, when a thread calls the wait() method, it releases the monitor or lock it was holding on that object, but when a thread calls the sleep() method, it never releases the monitor even if it is holding.

Coming back to yield(), it’s little different than wait() and sleep(), it just releases the CPU hold by Thread to give another thread an opportunity to run though it’s not guaranteed who will get the CPU.

Reading material two: https://www.cnblogs.com/aspirant/p/8876670.html

wait(): when a thread calls the wait() method, it releases the monitor or lock it was holding on that object

sleep(): when a thread calls the sleep() method, it never releases the monitor even if it is holding.

yeild(): it just releases the CPU hold by Thread to give another thread an opportunity to run though it’s not guaranteed who will get the CPU.

Guess you like

Origin blog.csdn.net/qq_41729287/article/details/113886883