Java Concurrency: What is concurrency? Users often take it for granted that computers can do more than one thing at a time.
Example 1: For example, you can listen to music, play a game and chat with others through a chat tool at the same time. If you are using a QQ music player to listen to music, play q-Q Doudi-main game, and chat with others using Q-Q, then it is actually three different software running at the same time, doing three different things.
Of course, we can’t deny that all three things can be done simultaneously: chat, games and music in one piece of software. For example, if you’re playing Q-Q-Doo-landlord so that you can play some music in the background while you chat with other poker players and say “Thank you for waiting for me”, this is actually an application that can do three different things at once.
In either case, what we should be aware of, and pointing to, is multitasking at the same time.
Java Concurrency Basics
Threads themselves have many advantages, such as the powerful ability of multi-processors, simpler modeling, simplified processing of asynchronous events, and more sensitive user interfaces. However, most of us are faced with security issues, such as:
public class Concurrence{ private int value; /* Returns a unique value */ public intgetValue() {returnvalue++; }}Copy the code
The problem with Concurrence is that if the execution time is wrong, then two threads will get the same value when calling getValue.
Although the increment operation value ++ appears to be a single operation, it contains three separate operations: reading value, value + 1, and writing value. Because of the run, it is possible to alternate operations between multiple threads, so that the two threads can read together so that they get the same value and both increment this value by one. As a result, the same value is returned in calls from different threads.
What is illustrated in Concurrence is a common concurrency security problem called race conditions. Race conditions occur when the correctness of a calculation depends on the alternate execution timing of multiple threads.
Introduction to Java concurrency and multithreading
In the old single-CPU era, a single task could only execute a single program at a point in time. Then came the multi-task stage, in which the computer could perform multiple tasks or processes in parallel at the same time. Although not in the true sense of the “same point in time”, but multiple tasks or processes share a CPU, and the operating system to complete the multi-task CPU switching, 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 a monopoly on all CPU time, all memory, and other computer resources. A good example of a program is to release resources when they are no longer in use so that other programs have a chance to use them.
Later, it developed into multi-threading technology, which enables multiple threads to execute in parallel within a program. The execution of a thread can be thought of as a CPU executing the program. When a program runs in multiple threads, it is as if more than one CPU is executing the program at the same time.
Multithreading is more challenging than multitasking. Multithreading is executed in parallel within the same program, so concurrent reads and writes are performed on the same memory space. This is probably never a problem in a single-threaded program. Some of these errors are also unlikely to occur on a single-CPU machine, as the two threads are never really executed in parallel. However, more modern computers come with multi-core cpus, which means that different threads can be executed in true parallel by different CPU cores.
If one thread is reading a memory while another thread is writing to that memory, what result does that thread get? Is the old value before the write operation? Or the new value after a successful write operation? Or half new and half old? Or, if two threads write to 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? So without proper precautions, any outcome is possible. And the behavior can’t even be predicted, so the outcome is uncertain.