Java Concurrent Programming Combat

Source: Internet
Author: User

Java Concurrent Programming Combat

The Fork/join framework is a framework that JDK7 provides for parallel execution of tasks, a framework that divides large tasks into parallel execution of subtasks, and ultimately summarizes each small task to get the result of a large task. We then use fork and join to understand the Fork/join framework. Fork is a large task divided into a number of sub-tasks in parallel execution, join is to merge the results of these subtasks, and finally get the result of this big task.

When using the Fork/join framework, you first need to create a Forkjoin task (basic tutorial Qkxue.net) that provides a mechanism for performing fork () and join operations in a task. Normally, we do not need to inherit forkjointask directly, just inherit its subclass, the Fork/join framework provides two subclasses: Recursiveaction is used for tasks that do not return results, Recursivetask is used for tasks that return results. Forkjointask need to be executed through Forkjoinpool.

The subtasks are added to the double-ended queue maintained by the current worker thread and into the head of the queue (Tengyun technology ty300.com). When there is no task in the queue for a worker thread, it randomly fetches a task from the end of the queue of other worker threads. (Work-Stealing algorithm work-stealing)

2. Lock downgrade

Lock demotion refers to the downgrade of a write lock into a read lock. If the current thread has a write lock, then releases it, and finally acquires a read lock, the process of completing this fragment is not called a lock demotion. Lock demotion refers to the process of holding (currently owned) a write lock, acquiring a read lock, and finally releasing (previously owned) a write lock. Refer to the following example:

Is it necessary to lock the read lock in the downgrade? The answer is necessary. The main purpose is to ensure the visibility of the data, if the current thread does not acquire a read lock instead of directly releasing the write lock, assuming that the other thread (T) acquires a write lock and modifies the data, then the front thread cannot perceive the data update of the threads T. If the current thread acquires a read lock, which follows the step of lock demotion, the thread T will be blocked until the current thread has used the data and freed the read lock before thread t can get a write lock for data updates.

Manuscripts: Diligent Learning qkxue.net

Read the full version of Java Concurrent Programming practical combat

Java Concurrent Programming Combat

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.