Java Multithreaded Learning Notes

Source: Internet
Author: User

Concept part:

1. Concurrency and Parallelism differences:

Concurrency (Concurrency): One processor "simultaneously" handles multiple tasks

Parallel (Parallelism): multiple processors "simultaneously" Handle multiple tasks

2. Advantages and disadvantages of multithreading:

Advantages of Multithreading:

Tasks can be processed in parallel, reducing the wait time for individual tasks

A thread is less expensive than a process or sub-process system

Easier sharing of resources between threads

Leverage CPU resources in multi-core scenarios

Disadvantages of Multithreading: Access violation lock contention – Deadlock – lock granularity context switch overhead synchronization/memory copy Overhead

JVM section:

"Tinking in Java" section:

1. Concurrency has demonstrable certainty, but is actually nondeterministic.

2. If no tasks are blocked, it makes no sense to use concurrency on a single-processor machine.

3. Some programming languages are designed to isolate concurrent tasks from each other, which are often referred to as functional language--erlang.

4. Syntax sugar: while (countdown-->0), private final int id = taskcount++; The use of the rear + + and--, the counter can be added and reduced operation, convenient and clear.

5. Various actuators (Executor):

Cachedthreadpool: Generally preferred, will be created on the same number of threads as required.

Fixedthreadpool: A good thread in advance, limiting the number of threads.

  Singlethreadexecutor is like a fixedthreadpool with a number of threads of 1 (it provides long-term survival and concurrency assurance, which is suitable for certain tasks). Singlethreadexecutor serializes all the tasks submitted to it.

6. If you want the task to return a value when it is completed, replace the Runnable interface with callable.

7.

Java Multithreaded Learning 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.