In-depth understanding of Java Virtual Machine Learning notes 10--concurrent Programming (ii)

Source: Internet
Author: User

Java's concurrent programming is implemented with three features that rely on the virtual machine's memory model:

(1). atomicity (atomicity):

Atomicity is the smallest operation instruction that cannot be divided, that is, a single machine instruction, atomic operation can only have one thread at any time, so it is thread-safe.

The Java memory model guarantees atomic operation of variables through the 6 operations of Read, load, assign, use, store, and write.

Long and double these two 64-bit length data types The Java Virtual machine does not enforce the atomicity of their read, load, store, and write operations, the so-called non-atomic protocol, However, the various commercial Java virtual machines currently implement the 4 non-atomic contract operation of the long and double data types as atomic. So access to read and write basic data types in Java is an atomic operation.

The guarantee of a wide range of atomicity is ensured by the lock and unlock operations as well as the synchronized synchronization block.

(2). Visibility (Visibility):

Visibility means that when a thread modifies the value of a shared variable, other threads can immediately know the change.

The Java memory model implements visibility by synchronizing the new value back to main memory after the variable is modified, and refreshing the variable value from the main memory before the variable is read.

Visibility is guaranteed in Java with three keywords, volatile, final, and synchronized:

Volatile: Ensures visibility by refreshing the value of the variable.

Synchronized: The sync block must clear the value of the variable in the working memory before it is locked by the variable lock, re-read the value of the variable from main memory, and unlock must synchronize the value of the variable back to main memory to ensure visibility before unlocking.

Final: Once the final modified field is initialized in the constructor and the constructor does not pass the this reference in, the value of the final field can be seen in other threads and can be accessed correctly by other threads without synchronization.

(3). Order (Ordering):

The ordering of threads is that: inside the thread, all operations are executed sequentially, and the operation is executed in a disorderly sequence, because of the delay between the working memory and the main memory synchronization.

Java uses the volatile and synchronized keywords to ensure the ordering of operations between threads.

Volatile prohibition command reordering optimization to achieve order.

Synchronized ensures ordering by allowing only one thread to lock it at the same time through a variable.

The implementation of the JDK thread is as follows:

(1). Kernal thread:klt, kernel thread, running in the kernel state, is directly supported by the operating system kernel thread, has the operating system kernel to complete kernel thread switching, kernel operation thread Scheduler ThreadScheduler kernel thread scheduling, Responsible for mapping kernel thread tasks to individual processors.

(2). Light weight PROCESS:LWP, a lightweight user process, is a traditional thread in programming, and each lightweight process is supported by a kernel thread.

(3). Users Thread:ut, user threads, run in the user state, completely by the user space Line libraries implementation, the kernel thread can not perceive the implementation of the user thread, user thread creation, synchronization, scheduling and destruction complete in the user state, do not need the kernel state support.

JDK threads are implemented based on the native threading model of the operating system, so the JDK version of the thread model depends on the mapping of the Java Virtual machine thread to the operating system thread, which is different on different platforms.

There are two ways of thread scheduling:

(1). Collaborative: The execution time of a thread is controlled by the thread itself, and the thread task executes after the active notification system switches to another thread to execute.

Advantage: The implementation is simple, the thread switching operation is known to the thread itself, there is no thread synchronization problem.

Cons: Thread execution time is not controllable, and if a thread is executing for a long time it may cause the system to crash without giving up CPU execution time.

(2). Preemptive: The execution time of each thread is assigned by the operating system, the operating system assigns each thread the execution time slice, seizes the time slice the thread executes, the time slice runs out and then re-seizes the execution time, the thread's switch is not determined by the thread itself.

Advantage: Thread execution time is controllable and the system crashes without a thread blocking problem.

Multithreading in the current JDK is a preemptive multi-threaded system, but it can be done by setting the thread priority and changing the execution time allocation probability of the thread.

Note: Because the thread priority of the JDK and the thread priority of the operating system are not one by one, it is recommended that you use only 1 (lowest priority), 5 (normal priority), and 10 (highest priority) three priorities.

In addition, the thread priority is simply the probability that the operating system assigns the thread the execution time, not absolute.

The state of the thread in Java is the dispatch relationship as follows:

(EXT) "In-depth understanding of Java Virtual Machine" Learning note 10--concurrent Programming (ii)

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.