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

How to understand Java concurrency

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

Share

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

This article introduces the relevant knowledge of "how to understand Java concurrency". In the operation of actual cases, many people will encounter such a dilemma, 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!

What is concurrency? Users usually take it for granted that computers can do more than one thing at the same time.

Example 1: for example, you can play games while listening to music while chatting with others through chat tools. Suppose you use a QQ Music player that is listening to music, playing qq fighting landlords, and using qq to chat with others, then it is actually three different software running at the same time, accomplishing three different things.

Of course, we can't deny that we can accomplish the above three things at the same time in one software: chat, games and music. For example, you are playing qq fighting landlords so that you can play pai and play background music. At the same time, you can also chat with other card friends and say, "Thank you for the flowers we've been waiting for." this is actually a software that can do three different things at the same time.

In either case, what we should know is that concurrency refers to performing multiple tasks at the same time.

Java concurrency Foundation

Thread itself has many advantages, such as taking advantage of the powerful power of multiprocessors, simpler modeling, simplified handling of asynchronous events, and more sensitive user interface, but most of us are faced with security issues, such as:

Public class Concurrence {

Private int value

/ * return a unique value * /

Public int getValue () {

Return value++

}

}

The problem with Concurrence is that if the timing is not right, the two threads will get the same value when calling getValue, an error demonstration:

Although the increment operation value + + appears to be a single operation, it contains three separate operations: read Value, value + 1, and write the result to Value. As a result of running, operations between multiple threads may be performed alternately, so the two threads may work together to perform read operations so that they get the same value and both add 1 to that value. As a result, the same value is returned in calls from different threads.

What is described in Concurrence is a common concurrency security problem, called race condition. A race condition occurs when the correctness of a calculation depends on the timing of alternating execution of multiple threads.

Introduction to Java concurrency and multithreading

In the past era of single CPU, a single task could only execute a single program at a point in time. After that, it develops to the multitasking stage, where the computer can execute multitasking or multiprocesses in parallel at the same point in time. Although it is not "the same point in time" in the real sense, but multiple tasks or processes share a CPU and leave it to the operating system to complete the switching of CPU between multiple tasks, so that each task has a chance to get a certain time slice to run.

With the new challenges of multitasking for software developers, programs can no longer assume the monopoly of all CPU time, all memory, and other computer resources. A good example of a program is to release these resources when they are no longer using them, so that other programs have the opportunity to use them.

Then developed to multi-threading technology, so that multiple threads can be executed in parallel within a program. The execution of a thread can be thought of as a CPU executing the program. When a program is running under multithreading, it is as if multiple CPU are executing the program at the same time.

Multithreading is more challenging than multitasking. Multithreading is executed in parallel within the same program, so concurrent read and write operations are performed on the same memory space. This may be a problem you never encounter in a single-threaded program. Some of these errors may not necessarily occur on a single CPU machine, because the two threads never get true parallel execution. However, more modern computers are accompanied by the emergence of multicore CPU, which means that different threads can be executed in parallel by different CPU cores.

If one thread is reading a memory and another thread is writing to that memory, what will be the result of the thread reading? Is it the old value before the write operation? Or the new value after a successful write operation? Or half the new and half the old? Or, if two threads write the same memory at the same time, what happens when the operation is complete? Is the value written by the first thread? Or the value written by the second thread? Or is it a mixed value written by two threads? Therefore, without appropriate preventive measures, any outcome is possible. And the occurrence of such behavior can not even be predicted, so the results are uncertain.

This is the end of "how to understand Java concurrency". Thank you for your 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

Development

Wechat

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

12
Report