Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the important concept of java concurrent programming

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "what is the important concept of java concurrent programming". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Concurrent VS parallelism

Multithreaded programming is also known as concurrent programming. And a concept like a twin brother is called parallelism.

The so-called concurrency is a concept that already exists before multicore, in which multiple threads execute tasks alternately.

On the other hand, parallelism refers to the simultaneous execution of multiple threads on the basis of multi-core.

Just like when we go to WC, there is only one toilet, and normally only one person is using it at a time, which is single-threaded execution.

If two or more people are waiting and the person who owns the toilet has constipation, use the next one for the principle of giving priority to short work, which is multithreaded concurrently for a while.

If the number of toilets is greater than the number of people waiting, people can use it directly and do it at the same time, which is multithreaded parallel execution.

Brush the wall

Suppose you currently want to paint a house, and there are more rooms in the house.

At the beginning, worker A was working alone, busy from morning to night every day, and the progress was slow. (single thread execution)

In order to improve efficiency, worker A found another helper, which we call worker B. (multithreading)

Of the two, An is better at mixing paint, while B is better at painting walls. The two men work together. Adjust the paint in An and start painting in B.

A put the paint in the bucket, and B takes the bucket and carries on the painting work.

The whole ingredient consists of three main steps:

water

Add paint

Stir

One day, B had something on his mind and was absent-minded. As soon as A poured the water into the bucket, B took the bucket and started painting with it, doing a lot of useless work (the result caused by incorrect execution sequence is called race condition).

A finds that what is in his bucket may be used by B when it is not ready (non-atomic operation). For this reason, A moved to a small room, one bucket of paint at a time before it was put into the coating area, and B took the prepared material from here to paint.

During the A batching period, the buckets are occupied by A, and B will no longer use the half-allocated material as before. We call a batching work of An an atomic operation. At the same time, it is considered that the operation of An is the operation of Happens-before B.

For the use of buckets between An and B, only one person can use them at a time. We call this operation mutually exclusive.

This kind of locking is realized by bucket possession, which is called implicit lock. In Java, it is called Monitor lock or pipe lock.

In order to ensure that he will not be disturbed by others in the small room, A will write the work on the door during each ingredient, please wait a moment, so that others will not enter the room and interrupt after seeing it. This form of lock is called explicit lock.

Assuming that the paint delivery of An and B is carried out in the coating area of the public area, then the material prepared by An in the small room each time is unknown to B. B can only use it if A puts the material in the public area. (this is the memory visibility often mentioned in multithreading)

Let's say that A put the material in the public area, and B didn't take it for a while and was taken away by someone who didn't know it. Shortly after that, An once again provided the expected public area, at this time B to pick up the material, for B, did not know that A had provided the material before. (this is an ABA problem often mentioned in concurrency)

That's all for "what are the important Concepts of java concurrent programming?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report