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 are the problems about concurrent programming in Java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "what are the problems about concurrent programming in Java?" interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the problems about concurrent programming in Java.

Why concurrency is needed

Concurrency is actually a decoupling strategy that helps us separate goals from timing. Doing so can significantly improve the throughput and structure of the application. Anyone who has done Java Web development knows that Servlet programs in Java Web work in a single instance and multithreaded mode with the support of the Servlet container, and the Servlet container handles concurrency problems for you.

Misunderstanding and correct solution

Common misunderstandings about concurrent programming are as follows:

Concurrency can always improve performance (concurrency can significantly improve program performance when CPU has a lot of free time, but when there are a large number of threads Frequent scheduling switches between threads will degrade the performance of the system)-there is no need to modify the original design for writing concurrent programs (decoupling of purpose and timing often has a great impact on the system structure)-there is no need to pay attention to concurrency when using Web or EJB containers (only by knowing what the container is doing, can you make better use of the container)

The following statements are the objective understanding of concurrency:

Writing concurrent programs adds extra overhead to the code-proper concurrency is very complex, even for simple problems-defects in concurrency are not easy to detect because they are not easy to reproduce-concurrency often requires fundamental changes to the design strategy.

Principles and skills of concurrent programming

The principle of single responsibility, separating concurrent related code from other code (concurrent related code has its own development, modification, and tuning life cycle).

Limit the data scope, two threads may interfere with each other when modifying the same field of the shared object, resulting in unexpected behavior, one of the solutions is to construct critical areas, but the number of critical areas must be limited.

Using data copies, data copies are a good way to avoid sharing data, and replicated objects are only treated as read-only. Java 5's java.util.concurrent package adds a class called CopyOnWriteArrayList, which is a subtype of the List interface, so you can think of it as a thread-safe version of ArrayList. It uses write-time replication to create copies of data to avoid problems caused by concurrent access to shared data.

Threads should be as independent as possible

Let threads exist in their own world and do not share data with other threads. Anyone with experience in Java Web development knows that Servlet works in a single-instance, multithreaded way, and the data associated with each request is passed in using the parameters of the service method (or doGet or doPost method) of the servlet subclass. As long as the code in Servlet uses only local variables, Servlet will not cause synchronization problems. The controller of SpringMVC does the same, and the objects obtained from the request are passed in as parameters of the method rather than as members of the class, which is obviously the opposite of what Struts 2 does, so the Action class as a controller in Struts 2 has one instance for each request.

At this point, I believe you have a deeper understanding of "what are the problems of concurrent programming in Java?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report