Learning (1) the basic concepts of Java Concurrency

Foreword

In operation often concurrent exposure to the environment, but has a good frame existing at the bottom of the package, can be called directly. But there are always some strange scenarios need to manually implement concurrent, so it is important to understand the principle. This paper used to record important point in learning or thinking concurrent processes.

This article only discuss the basic concepts, designed to record some of their own understanding of the concurrent use more detailed and understandable text.

What is complicated by

Concurrent (Concurrency) refers to a system at the same time can handle multiple tasks simultaneously, and that only one task is running.

In fact, for single-core CPU can not be achieved in terms of running multiple tasks simultaneously true. However, due to the fast CPU operation within a short time unit (for example, 0.001 seconds) to perform a task, executing the switch immediately to the next task. This looks like a macro to run multiple tasks at the same time.

Parallel with ** (Parallel) ** difference: Parallel refer to the same time you can run multiple tasks simultaneously. Modern multi-core processors support the parallel operation. The system supports multiple concurrent tasks emphasized the same time exist; a parallel emphasis on systems to support multiple tasks running simultaneously.

CPU cores

Can be understood as a multi-processor, dual-core CPU From a procedural point of view is equal to a shared resource may be two single-core processors. For a single-core processor, at any one time can only handle one task; however, multi-core processor can simultaneously process multiple tasks at the same time. Since the basic current CPU support so-called hyper-threading , such that the core can run a physical (such as Intel's i5 processor core 4 are usually thread 8, the thread 6 core 12) while achieving both tasks logically, the program angle a look at the core of the system while at the same time the number of tasks can be run as: CPU Hyper-Threading number * number of CPU.

Threads and processes

basic concepts

Process (Process) is a computer program run on a set of data on the activities of the system is the basic unit of resource allocation and scheduling, is the underlying operating system architecture, but also the thread of the container.

The thread (Thread) is the minimum unit of the operating system can schedule calculation. It is included in the process, the actual operation of the unit process. A thread refers to a single control flow of a process sequence, a process can be complicated by a plurality of threads, each thread in parallel to perform different tasks.

An operating system with more than one process that has at least one thread. A program has one or more processes, usually used as a process, but such as Chrome, Firefox and other applications will have multiple processes. Resources can not be shared between processes in general, because the process has a separate memory space, resource allocation, independently of each other between multiple processes; but the thread was naturally shared memory space, so multi-threaded applications biggest problem is how to avoid resources are multiple threads access, and multi-process applications biggest problem is that the inter-process communication.

Thread scheduling

The basic unit of CPU scheduling is the thread. For scheduling process is there are different ways among different systems, not to discuss here.

For a single-core CPU, the system will assign a so-called executable threads for each time slice (the current thread means executable time), when the thread execution exceeds the execution time or leave runnable, a CPU will be switched to the next thread, if the thread is not finished before the execution, execution is suspended, waiting for the next time slice sliced; there is a division between the thread priority, high-priority thread will have more chance to get into time slices. For most servers, the use of on-chip Xeon range of services usually have more than 6-core, coupled with the blessing Hyper-Threading technology, which can handle at least 12 threads, thread gets a time slice to frequency also increased significantly. Have a high CPU clock speed, i.e. the clock frequency in its instructions for performing calculations, the higher the frequency, the faster the frequency, a time unit number of extra instructions can be executed even greater, the time it takes a thread sheet will will become less, all the threads will be scheduled more frequently.

For a server, the number of threads the program is not the better. The number of threads is too large (thousands of thousands) is clearly inappropriate, creating a thread will consume a lot of system resources, CPU context switching costs will be spent more, a large number of threads will wait a long time in the state can not be timely deal with. Suitable number as follows:

= CPU cores threads / (l - blockage factor) = blockage factor blocking time / (obstructive calculation time + time)

It is assumed that each thread blocked for 8 time units, the calculation time is two time units (for most I / O system, which is generally greater than the calculated time blocking time, that is to say threads access the I / O is most of the time wait state), then the blocking factor to 0.8; that for a dual-core processor, its best to open ten threads, in order not to waste system resources, but also to take into account all the threads. (Lack of understanding of the operating system, file system I have been do not think blocking is used when CPU it? So being the first to understand it)

Extended: coroutine (Coroutine) known shred, the thread will be appreciated that the threads. A process can have multiple threads, one thread at the same time also can have multiple coroutines. In some highly concurrent native languages (such as golang ) natural support, Java also has third-party libraries ( Quasar ) support. Rely on thread switching executed by a CPU overhead context switch occur, and the maximum number of threads supported limited, but is coroutine switching program's internal implementation, switching overhead does not exist, and exists in a coroutine which thread, need not lock, write without worrying about conflicts of variables, so its efficiency is much higher than threads. A single server may support up to hundreds of threads, but supports millions of coroutines, so can greatly enhance the ability of the system concurrently.

State of the thread

Thread owns five basic states:

  • Created: just a thread is created;
  • Ready: the current thread in a runnable state, waiting to acquire time slices;
  • Run: thread gets the time sheets, running;
  • Obstruction: for some reason to abandon the current thread CPU, and the stage could not get CPU time slice;
  • Death: the thread is finished or an exception is aborted.

Argument is relatively simple, but roughly the same. For a discussion of different opinions on the Web these states, here only briefly describe abstract concepts.

Blocking and non-blocking

The first is blocked . Several state of the thread, the blocking state is a problem faced by programs typically require. Due to blockage causes the current thread is suspended, giving up CPU time slice, if all the threads have to face blocked for a long time, the performance of the system will face a bottleneck.

When a thread access to some resources (I / O resources), because the current can not get the resources, then the current thread to give up the right to use the CPU, waiting for a resource available. This state resources were waiting for call blocking. When a thread is blocked, doing nothing. Once the resources are available, it will trigger an event, the current thread becomes runnable state, waiting for re-allocation of CPU time slice. It's just understanding the macro to the underlying terms, things to do very complex system. When faced with the same threads synchronize access to variables, the process of waiting for the lock distribution is also known as blocking state. In short, the thread waits for the right to use the shared resources of this stage is called a blocking state.

Many of our programs have to face the problem of congestion. For example file I / O, network I / O, or other hardware resources such as a printer. For example, when accessing the resource file, java is to be accessed via the file stream. FileInputStream.read()This method in the source code is actually called the native method implemented by a native C code: FileInputStream.read0(). This method reads a byte from the file into jvm memory of them, the process is blocked (can be viewed in the official document of java, view the source code can be seen in the corresponding comment when using IDE development). From the lower-level approach to understanding the obstruction, it should be java program call read () method reads bytes sent upon request to the local file system, and this byte is to be prepared by the file system. File system and JVM actually belong to a different program, which is a different process, the file system JVM to wait until you are ready this byte, the CPU suspends the current Java thread, the current thread is blocked, that is not after the execution, the state can not get a slice of time, waiting for this byte is ready, and then setting the current thread can run state, it is ready state. When this thread again to get the CPU time slice, this program will continue to run down. Network I / O as well, such as HTTP requests, JDBC database access.

For service providers usually run, the blocking time consuming often longer than the program execution consumption, such applications also known as I / O-intensive applications , such as information websites, query system; however, another type of application , such as data analysis, the application of such depth study, they will not face the more I / O operations, data calculation is more, such an application is called compute-intensive applications . For the former, enough to set thread that makes the program to take advantage of CPU resources; and for the latter, directly enhance the hardware capabilities of parallel computing, and then set a reasonable number of processes or threads (usually not more than the number of cores) in order to ensure the efficiency of the program because the operation thread switching is also required for the CPU resource consumption.

If the program is almost no jam, the concurrency model will not have much meaning, then the number of threads only need to set the number of CPU cores just fine. If it is a single-core CPU, direct serialization of execution without the need to consider concurrency issues.

Blocking and non-blocking state are emphasized thread. Non-blocking naturally refers to the current thread in a critical situation should not block, can directly execute the program down. Such as reading the file, if the read()method is non-blocking, then the java initiation request, return the result directly read, the file system may be relatively fast, this time a byte has been read out for direct return, or can not read and to take returns -1, then the program will continue execution. For non-blocking request, usually you need to do more work in the program to deal with the integrity of resource acquisition. For a discussion of non-blocking, then continued.

Synchronous and asynchronous

Synchronous and asynchronous, from the conceptual point of view, it is concerned that the message communication. Synchronization emphasize makes a call or a request, you must wait for it to return a result to be able to perform the next step. Asynchronously it emphasized that the next step directly after the operation request does not need to return the result immediately concerned. For programmers, the two concepts are explained in the abstract perception level, while at the code level, multi-threaded or multi-process and in fact has little to do. For example, a resource server, the call interface simply returns the cached good pictures, text, etc., resources are read-only (network diagram bed, content publishing system widely used), services are usually provided concurrently, which opened more than process / multi-threaded, but the service itself is synchronized because the calling interface will wait for the return of resources; and usually rely on asynchronous callback function, event polling mechanisms to achieve. A typical example NodeJS, its mechanism is a single-threaded asynchronous model, relying on internal polling to implement asynchronous event.

Event polling can be understood as finished after the implementation of the corresponding event registration, then begin an endless loop in the main thread, polling is used to monitor whether all registered events is triggered. Trigger mode is very simple to operate identifies a resource shared by a thread triggered when polling is detected identified as a trigger state, the event began to call the appropriate function. After machining, the cycle continues. In fact NodeJS also rely on itself to achieve a multi-threaded, it is only available to the programmer aware of only a single thread only. Its core is the Chrome browser's V8 engine, in fact, the browser execute Javascript is true, the code must be written to implement a single-threaded, but the browser built-in polling thread, the event listener thread and so on. Event polling model is ideal for interface program, using fewer threads / processes and resources to maintain efficient operation of the program.

There are also many other types of models, such as synchronous blocking (typical of Java I / O stream), non-blocking synchronous, asynchronous blocking, non-blocking asynchronous, its implementation also diverse, in depth discussion will not be.

At last

In this paper the author compares with just colloquial word simply explains some of the basic concepts of concurrent programming in common explanation is not very complete, after all, you really want to completely explain where all descending from the bottom to have to write all this abstract concept, then very thick book the job, I just hope this article provides a summary of the theory is more able to understand Java concurrent programming related notes to be recorded next. Many may not understand fully (and perhaps access to information is not complete), which may be wrong, correct me also welcome.

Understanding sources

  • "Java programming ideas" Chapter 21: Concurrency
  • Baidu Encyclopedia, Wikipedia
  • Search engine to search various types of blog

Guess you like

Origin juejin.im/post/5e10bfcce51d4541625a4fdb