What's the problem with multithreading in Java's concurrent programming?

Source: Internet
Author: User

In one of my previous articles, "someone asks you what the Java memory model is, send it to him." Article, introduced the Java memory model, through this article, everyone should know the Java memory model of the concept and role, in this article, in Java concurrent programming, often encounter three problems, namely, atomicity, consistency, and ordering problems.

The above article introduces briefly, for various reasons can lead to the problem of atomicity, consistency and order in multi-threaded scenarios. But not in depth, this article on the basis of the previous, and then look at the concurrent programming, where are these problems?

First of all, let's start with the operating system and get some basic knowledge first.

CPU time slices

Many people know that now we use the operating system, whether Windows, Linux or MacOS is actually a multi-user multi-task time-sharing operating system. "Users" using these operating systems can do many things "at the same time", which is already a daily habit, and does not feel anything special.

But in fact, for a single CPU computer, in the CPU, the same time can only do one thing.

In order to look like "do many things at the same time", time-sharing operating system is to divide the CPU into the same length of time, that is, "time slice", through the management of the operating system, these time slices in turn assigned to each "user" use.

If a "user" before the end of the time slice, the entire task is not completed, the "user" must go into the ready state, abandon the CPU, waiting for the next round of the cycle. At this point the CPU is assigned to another "user" to use.


Different operating systems, in the selection of "user" allocation time slice scheduling algorithm is not the same, commonly used have fcfs, rotation, SPN, SRT, HRRN, feedback, etc., because this is not the focus of this article, it does not unfold.


Processes and Threads

The previous introduction of CPU time slices mentioned that the CPU will be based on different scheduling algorithm to allocate time slices to the "user", where the "user" in the previous refers to the process, with the continuous development of the operating system, now generally refers to the thread.

In an operating system with no threads in the past, the allocation and execution of resources is done by the process. With the development of technology, in order to reduce the overhead caused by the process switching, to improve the concurrency capability, the operating system introduces threads. To divide the work that was originally part of the process into the allocation of resources, while the thread is responsible for execution.

In other words, a process is the basic unit of resource allocation, and a thread is the basic unit of dispatch.

Concurrency issues in multi-threading

Having learned the basics of hardware and operating systems, let's look at the concurrency problems in multithreaded scenarios.

About atomicity, visibility, and ordering in concurrent programming I've covered it in the article "Memory model".

It is mentioned that the problem of cache consistency is actually a visibility problem. and processor optimization can lead to atomicity problems. The order rearrangement causes the problem of ordering. Some readers are not very understanding of this part. Because the previous article mainly introduced the memory model, and did not unfold the analysis, just gave a conclusion, here again for this part in-depth analysis.

Because the cache consistency problem causes the visibility problem, in the "memory model" introduced very clear, here does not repeat, mainly unifies this article to analyze the atom question and the order question.

Atomicity issues

We say that the problem of atomicity, in fact, refers to the operation of multi-threaded scenarios if atomicity is not guaranteed, resulting in inconsistent processing results and expectations.

As we mentioned earlier, threads are the basic unit of CPU scheduling. CPU has the concept of time slices, according to different scheduling algorithms for thread scheduling. So in a multithreaded scenario, an atomic problem occurs. Because when a thread executes a read overwrite operation, when the time slice is exhausted after the read change is performed, it is required to abandon the CPU and wait for a rescheduling. In this case, the read rewrite is not an atomic operation.

In a single thread, it doesn't matter if a read overwrite is not an atomic operation, as long as the thread is dispatched again, the operation can always be done. However, there may be problems in the multithreaded scenario. Because multiple threads may operate on the same shared resource.

For example, the classic i++ operation, for a simple i++ operation, a total of three steps: Load, Add,save. Shared variables are manipulated by multiple threads at the same time, so that read rewriting operations are not atomic, and the values of shared variables are inconsistent with expectations after the operation, for example: if I=1, we perform two i++ operations, we expect the result to be 3, but it is possible that the result is 2.

The problem of order

Moreover, we know that, in addition to the introduction of time slices, due to processor optimization and instruction rearrangement, the CPU may also perform random execution of input code, such as Load->add->save may be optimized to load->save->add. This is the problem of order.

Or just the i++ operation, in the case of satisfying the atomicity, if the order is not satisfied, then the results may not be what we want.


Summarize

In this paper, we mainly introduce the causes of atomicity and ordering in concurrent programming, and refer to the memory model for visibility. The solutions to these three problems are also introduced in the memory model, and more can be consulted on multi-threaded books. Hollis Follow-up will also be more articles in-depth analysis, please look forward to.

What's the problem with multithreading in Java's concurrent programming?

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.