1. Concurrency can often improve the performance of programs on a single processor
In fact, the overhead of executing program locks concurrently on a single processor is greater than the sequential execution. However, in sequential execution, the program can sometimes be unable to continue because of certain conditions (usually I/O problems), called thread blocking, and if there is no concurrency, the program will stop. While using concurrency, one task is blocked and other tasks can continue to execute, which guarantees the completion of the program. Therefore, it is not necessary to execute the program concurrently on a single processor if it is determined that no tasks will be blocked.
2. Concurrency on a single processor The most common example of performance improvement is event-driven programming
The most attractive amount of concurrency is the ability to produce a responsive user interface, such as a program that will run an operation for a long time, but also to handle the user's input at any time, and it would be unwise to add a program that processes user input in every segment of the long-term task. and creating a separate thread to handle user input is a great implementation.
3. The operating system isolates the process, but Java does not, so the most difficult thing about Java multithreading is to coordinate the access of multiple threads to the same block of resources, ensuring that resources are not accessed by multiple threads at the same time.
4. Four the difference between the median pool (newcachedthreadpool,newfixedthreadpool,newscheduledthreadpool , Newsinglethreadexecutor )
New thread () creating threads There are a number of drawbacks: a. Each new thread creates a poor performance for the newly created object. B. Lack of unified management of threads, may be unlimited new threads, competing with each other, consuming excessive resources resulting in the crash. C. Lack of additional functionality, such as timed execution, periodic execution, and thread interruption.
Newcachedthreadpool: Creates a cacheable thread pool that can flexibly reclaim idle threads if the thread length exceeds the processing needs, and create a new thread if it is not recyclable (preferred)
Newfixedthreadpool: fixed-thread pool, which controls the maximum number of concurrent threads, and the exceeded threads are queued
Newscheduledthreadpool: Timed thread pool to support timed and recurring task execution
newsinglethreadexecutor : A single thread pool that performs work only with unique threads, with all tasks executing in FIFO
Java Multithreaded notes