Good Java programmer learning route of Java Concurrency

Good Java programmer learning route of Java concurrency, what is complicated by? Computer users often think that at the same time can do more things for granted.

Example 1: For example, you can listen to music while playing the game, while also chatting with others through chat tools. Suppose qq music player you use to listen to music, play the game Landlords qq, and do not use qq chat, so this is actually three different software running at the same time, complete three different things.

Example 2: Of course, we can not deny that, in a software, and can be completed more than three things: chat, games and music. For example, you are playing qq Landlords, for playing cards, playing background music, and you can also chat with other players out, say something. "I like the flowers have wilted," which is actually a software that can be completed simultaneously three different things .

  Either way, we should know is that concurrency refers to perform multiple tasks at the same time.  

java concurrent basis

Thread itself has many advantages, such as the ability to play a powerful multi-processor, modeling easier, simplify processing of asynchronous events, is more sensitive to the corresponding user interface, but the more we are faced with security issues, such as:

public class Concurrence{

    private int value;

    

    / * Returns a unique value * /

    public int getValue(){

        return value++;

    }

}

Concurrence of the problem is that if the execution timing was not right, then the two threads when calling getValue get the same value, error demonstration:


While the increment value ++ appears to be a single arithmetic operation, but it contains three separate operations: reading Value, value + 1, writes the result Value. Since the operation, the operation may be performed alternately between a plurality of threads, so that the two threads may perform a read operation colleagues, so that they get the same value and this value will be incremented by one. As a result, it returns the same value in calling different thread.

Concurrence is described in a common concurrency security issues, called a race condition. When the accuracy of a calculation performed alternately depending on the timing of a plurality of threads, so a race condition can occur.

Java concurrency and multithreading Introduction

In the past era of a single CPU, single task at a time can only perform a single program. After the development stage to multi-task, the computer can perform multiple tasks in parallel at the same point in time or process. While not the "same time" in the true sense, but more tasks or processes to share a CPU, and handed over to the operating system to complete multiple tasks to run between the CPU switch, so that each task has a chance to get some time slice to run.

With the new multi-tasking challenge for software developers to bring the program is not able to assume all the exclusive CPU time, memory, and all the other computer resources. A good example is a program to release them when they no longer use these resources to enable other programs to have the opportunity to use these resources.

Then later developed into multi-threading technology, so that an internal program to have multiple threads execute in parallel. A thread of execution may be considered as a CPU executing the program. When a program runs in a multi-threaded, if there are multiple CPU while executing the program.

Multithreading is more challenging than multitasking. Multithreading is concurrent read and write operations in parallel with an internal program execution, and therefore have the same memory space. This could be a problem in a single-threaded programs never encounter. Some of these errors may not appear on a single CPU machine, because the two threads never get true parallel execution. However, more modern computer accompanied by the emergence of multi-core CPU, which means different parallel execution of different threads can get a real sense of the CPU core.

If a thread is in a memory read another thread forward the memory write operation, a read operation that carried out that thread will get the result? Is the old value before writing? Or write the new value after a successful operation? Or a half old half new value? Or, if two threads write to the same memory, after the operation is completed will be the result? The first value is written by one thread? Or write the value of the second thread? Or a mix of the two threads value written? So if there is no appropriate precautions, any result is possible. And the occurrence of such acts can not even predict, so the result is uncertainty.


Guess you like

Origin blog.51cto.com/14249543/2402705