Java Concurrent Programming 01 concurrent Considerations __ Algorithm

Source: Internet
Author: User

What to pay attention to when doing concurrent programming. Context Overhead

CPU threads are allocated different time slices through the scheduling algorithm, and when a thread's time slice is exhausted, the operating system pauses the thread, saves the thread's corresponding information, and then randomly selects a new thread to execute, a process called "thread context switching".

Each time a context switch is performed, the execution state of the current thread needs to be saved and the previous state of the new thread loaded. If context switches are frequent, the CPU spends more time on the context switch, and the actual processing time of the task decreases.

How to reduce context switching.

reduce the number of threads . However, if the number of threads is already less than the CPU core, each CPU executes a thread, which logically does not require context switching, but that is not the case.

controls the number of threads on the same lock. If multiple threads share the same lock, the other thread is blocked when a thread gets the lock, and when the thread releases the lock, the operating system selects one execution from the blocked thread, and then the context switch occurs.
Therefore, reducing the number of threads on the same lock can also reduce the number of context switches.

with no lock concurrent programming. if reducing the number of the same lock thread can reduce the number of context switches, if the lock is not used, it can avoid the context of the competition lock generated switch. Different strategies need to be selected according to the following two scenarios:

Tasks that require concurrent execution are stateless: the task that the hash fragment 
requires concurrent execution is stateful: CAS algorithm 
Deadlock Problem

A deadlock occurs when multiple threads wait on each other for resources already occupied by each other.

How to avoid deadlocks.

Do not nest multiple locks in a single thread;
do not nest multiple computer resources in a single thread;
lock and resource timeout 
if you have to nest multiple locks or consume multiple resources in a thread, you need to give the lock, the resource, and the timeout time. So as to avoid indefinite waiting.
Communication Mechanisms

There are two ways to communicate between threads: Shared memory and message delivery.

Shared Memory: This approach has the disadvantage of requiring programmers to control the synchronization of threads, that is, the order in which threads are executed.

Messaging: Because the execution order is done by the concurrency mechanism, programmers do not need to add additional synchronization mechanisms, but they need to declare the code that the message sends and receives.

To sum up: for the shared memory of the communication, the need for the display of synchronization, implicit communication, and for the communication mode of message delivery, the need for implicit synchronization, display of communication.

Java uses shared memory as a way to implement message delivery between multithreading. As a result, programmers need to write extra code for synchronization between threads.

Java uses shared memory to implement messaging, and shared memory relies on synchronization. Java provides synchronization for synchronized, volatile keyword implementations. In addition, the volatile keyword also has some additional features.

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.