"Java Concurrent Programming Practical" reading notes (a)-----introduction

Source: Internet
Author: User

History

Earlier computers did not contain the operating system, and only one program was executed from beginning to the other, and the program had access to all of the computer's resources. As the computer develops, the operating system appears so that the computer can run multiple programs at the same time, and each program runs within a separate process. Why did you do it? This is mainly based on the following reasons:

1. Resource UtilizationWhile the program waits for the computer to execute another program, the same resource can be used by different programs, which undoubtedly increases the utilization of resources.
2. Fair SexDifferent users and programs have the same rights to the computer, and each program shares resources through time, rather than waiting for the last program to run and then start the next program.
3. Ease ofEach program runs separately and communicates with each other when necessary, which is easier to accomplish than a single program does all tasks.

Since the computer from a single task to multi-tasking, the program can naturally be developed from the traditional serial program to concurrent programs .

The advantages of the serial program are simple and intuitive. But also because of the above three reasons to cause the emergence of threads . The above-mentioned computers can run programs within a separate process, while threads allow multiple program control flows within the same process, and multiple threads of a program can be run concurrently by multiple CPU schedules. This is equivalent to the computer executing multiple programs (processes) concurrently, and the program executes multiple threads at the same time. The thread that is running within the process.

Threads are also called lightweight processes. Today's operating systems are scheduled on a thread-by-unit base. Because of the sharing of resources between threads, if there is no explicit synchronization mechanism, it can cause inter-thread execution disturbances, data errors or failures, resulting in exceptions. This is often the difficulty of concurrent programming.

In my opinion, the so-called concurrent programming is the use of threads. Threads can switch many asynchronous workflows into serial workflows, while concurrent programming synchronizes them in asynchronous situations to meet demand.

So what are the advantages and risks of threading?

Threading Benefits

Threads can effectively reduce program development and maintenance costs and improve performance. Reduce the complexity of code and better simulate human working methods.

Advantage 1The powerful multi-processor multi-processor system is becoming more and more perfect, which makes threading more obvious. Because different threads can be dispatched by multiple processors at the same time, especially during I/O operations, the latency of the program is reduced, and multiple processors increase the efficiency of the program execution resulting in increased system throughput.
Advantage 2Reduce modeling difficulty It's always easier to do the same thing than to do more than one thing. The use of Thread Bar program task refinement, multi-tasking to multi-threading, each thread task single, when necessary to communicate with each other, than single-threaded back and forth to switch tasks cost less.
Advantage 3. Simplifying asynchronous event handling when a request waits for a response, does the entire program wait? This means that other requests will be halted. It's like having to reply to a conversation to say a word, which is obviously unreasonable. If each request is processed by one thread, the thread independently does not affect each other and resolves the problem. Of course, if the chat reply must know what the other said, pay attention to thread safety.
Advantage 4Response to a more agile user interface The traditional GUI program is single-threaded, and if one of the user events is handled too long, the entire program will be stuck and the user experience gets worse. If some time-consuming operations are assigned to a separate thread, the other events are still wired, which makes the user interface smoother. Thread risk

Threading support is a double-edged sword, and the development of concurrent programs also brings a higher level of demand to programmers.

Risk 1Security issues concurrent programming may cause errors that will not occur with single-threaded programs.
Risk 2The activity of active sexual issues is defined as "something right is bound to happen". An infinite loop in a single thread is a matter of activity. If thread a waits for thread B to release the resource it holds, thread B has not been freed, and thread A will wait until it is active.
Risk 3Performance problem performance is "the right thing to happen as soon as possible." A good concurrency design can improve performance, and conversely, it degrades performance, after all, startup threads also consume resources.

There is a good example of security:

value++;

Only this line of code, in a single-threaded programming completely no problem, but in multi-threaded can have unintended consequences.

Execute this line of code in three steps: 1. Read value, 2.value value plus 1;3. Assigns the value of the plus 1 back to value. The program may be alternately executed by multiple threads, during which two threads read value at the same time, thereby getting the same value and adding 1 to it, the result is that different threads get the same value. And our expectation is that this value has been added two times.

Every Java program uses threads, and immediately you create threads in the program without displaying them, and the framework that you use is still creating threads that must be thread-safe.

"Java Concurrent Programming Practical" reading notes (a)-----introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.