1, why do I need concurrency?
There is no operating system in the earlier computer, a computer executes only one program from beginning to end, if the task of the program is to perform CPU calculation before writing the result to the file. Then, when the CPU is calculated, the IO module of this computer is idle and the CPU of the computer is idle when the result is written to the file.
For this reason, the operating system is introduced. This way, you can run multiple programs on a single computer. By the operating system to manage the operation of multiple programs, one program in the CPU calculation, while another program can do IO operations. Increased utilization of the computer (CPU and IO operations can occur at the same time)
After the introduction of the operating system, the concept of process inevitably arises. Because the operating system manages several programs, each running program can be represented by a process (the dynamic nature of the process).
Then why do you need a thread?
① processes need to communicate, need to share some data, need collaboration ... These threads are more convenient to "express". Because the address space of different processes is independent of each other, one process cannot directly access the address space of another process. Multiple threads within a process share this process state space, which is much more convenient if the sharing of data is implemented at the thread level.
② further improve resource utilization---this is somewhat analogous to the transition from a computer without an operating system to a computer with an operating system---here is a transition from no thread to a wired thread.
2, we are because of the convenience of data sharing ... Some reasons introduce a thread, so when multiple threads access a variable variable at the same time, which state does this variable handle? There is a thread-safety issue.
3, what is thread safety
Discusses thread security for a class. The core is correctness. That is, the behavior of a class is exactly the same as its specification: that is, I have written a class, what functions this class has, what needs to be done ... These are the specifications of the class, and when multiple threads run the code for this class, the execution effect is consistent with the specification description.
This class is thread-safe when multiple threads access a class, regardless of how the runtime environment is scheduled or how those threads will be executed alternately, and if no additional synchronization or coordination is required in the keynote code, and the class is able to behave correctly.
4, stateless object
5,race Condition race condition
① read--Modify--write
②check--then--act
Basic concepts of JAVA concurrent Programming Learning (1)