Java Multithreaded notes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.