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 knowledge points related to Java concurrency

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

Share

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

This article mainly introduces "what are the relevant knowledge points of Java concurrency". In the daily operation, I believe that many people have doubts about the relevant knowledge points of Java concurrency. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what are the knowledge points related to Java concurrency?" Next, please follow the editor to study!

1. The problems solved by concurrency can be divided into "speed" and "design manageability".

2. Concurrency usually improves the performance of programs running on a single processor.

It sounds counterintuitive. If you think about it carefully, you will find that the cost of concurrent programs running on a single processor should indeed be higher than the cost of sequential execution of all parts of the program. because it increases the cost of so-called context switching (switching from one task to another).

What makes the problem a little different is blocking. If a task in the program cannot continue to execute because of some condition outside the control of the thread (usually Icord O), then we say that the task or thread is blocked. If there is no concurrency, the entire program will stop until the external conditions change. But if you use concurrency to write

Program, then when one task is blocked, other tasks in the program can continue to execute, so the program can continue to execute forward. In fact, from a performance point of view, there is no point in using concurrency on a single processor machine if no tasks are blocked.

3. The most direct way to achieve concurrency is to use processes at the operating system level. A process is a self-contained program that runs in its own address space.

4. The operating system usually isolates processes from each other, so they do not interfere with each other, which makes it relatively easy to program with processes. In contrast, concurrent systems like those used by Java share resources such as memory and Ibank O, so the most basic difficulty in writing multithreaded programs is to coordinate these resources between different thread-driven tasks.

Sources are used so that these resources are not accessed by multiple tasks at the same time.

5. The thread mechanism of Java is preemptive, which means that the scheduling mechanism will periodically interrupt the thread and switch the context to another thread, thus providing a time slice for each thread, so that each thread will be allocated a reasonable amount of time to drive its task.

6. A thread is a single sequential control flow in a process, so a single process can have multiple tasks executed concurrently.

Knowledge point expansion

Publish: an object enables it to be referenced by code outside the current scope:

Common form: store a reference to an object in a public static domain; return a reference in a non-private method; publish an instance of the inner class, including a reference.

Escape: publish an object when it is not ready.

Don't let the this reference escape in the constructor. For example, start a thread in the constructor and the thread contains a reference to the object.

Synchronization container: access through all the states of the container, Vector, Hashtable,Cllections.synchronizedMap | List

Concurrency containers: ConcurrentHashMap,CopyOnWriteArrayList,ConcurrentLinkedQueue, BlockingQueue

The advantages of list's random access feature.

Blocking added blocking get set operations

ConcurrentHashMap: detached locks provide high throughput for concurrent access with almost no loss of access performance for a single thread. Returns an iterator with weak consistency.

The weak consistency of the iterator detects changes to the container after the iterator is generated.

The concurrency container size (), isEmpty () weakens and returns approximate results.

CopyOnWriteArrayList: copy every time you modify the container. The iterative requirements are greater than the modification requirements.

The producer-consumer model uses bounded blocking queues to decouple the code of producers and consumers.

The Executor task execution framework implements the producer-consumer model.

SynchronousQueue:put waits for consumers to be available, and take waits for producers to be available, which is suitable for scenarios where consumers are sufficient.

At this point, the study of "what are the relevant knowledge points of Java concurrency" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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