Early computers did not include an operating system. Only one program was executed from beginning to end, and this program had access to all resources of the computer. With the development of computers and the emergence of operating systems, computers can run multiple programs at the same time, and each program runs in a separate process. Why do you do that? Mainly based on the following reasons:
Since computers have developed from single tasks to multi-tasks, programs can naturally develop from traditional serial programs to concurrent programs.
The advantage of serial programs is that they are simple and intuitive. But the above three reasons also prompted the emergence of threads. The above computers can run programs in separate processes, and threads allow multiple program control flows to exist in the same process. Multiple threads of a program can also be scheduled to run by multiple CPUs at the same time. This is equivalent to the computer executing multiple programs (processes) at the same time, and the program executing multiple threads at the same time. That is, running threads within the process.
Threads are also called lightweight processes. Today's operating systems are scheduled with threads as the basic unit. Since resources are shared between threads, if there is no clear synchronization mechanism, it will lead to disordered execution between threads, data errors or failures, and exceptions. This is often where the difficulty lies in concurrent programming.
From my point of view, the so-called concurrent programming is the use of threads. Threads can convert many asynchronous workflows into serial workflows, and concurrent programming is to achieve synchronization in asynchronous situations to make it meet the needs.
So what are the advantages and risks of threads?
Threads can effectively reduce program development and maintenance costs and improve performance. Reduce code complexity and better simulate human working methods.
Thread support is a double-edged sword, and the development of concurrent programs also brings higher level requirements to programmers.
There is a good example about security:
value++;
With just this line of code, there is no problem at all in a single-threaded program, but unexpected consequences may occur in multi-threaded programs.
There are three steps to execute this line of code: 1. Read value; 2. Add 1 to value; 3. Assign the added value back to value. The program may be executed alternately by multiple threads. During this period, if two threads read the value at the same time, get the same value, and add 1 to it at the same time, the result is that different threads get the same value. And our expectation is that this value is added twice.
Every Java program uses threads. Even if you create threads without explicitly creating them in the program, the framework you use is still creating threads. These threads must be thread-safe.
The above is the detailed content of A brief introduction to JAVA. For more information, please follow other related articles on the PHP Chinese website!