Java Threading Knowledge Supplements

Source: Internet
Author: User
Tags thread class volatile

Knowledge Review

Processes and threads are two concepts that are often referred to. Processes have separate code snippets, data spaces, threads that share code snippets and data spaces, but have separate stack spaces. A thread is the smallest unit of operating system scheduling, and typically a process contains one or more threads. Multi-threaded and multi-process can be implemented concurrently, such as Nginx using multi-process mode, tomcat using multithreading, Apache support mixed use. Multi-process and multi-threading can be used in languages such as C + +, while only multithreading is used in Java.

In Java, the only way to create threads is to create an instance of the thread class and invoke the instance's start () method to start the thread.

Java Threading Implementation

Prior to JDK 1.2, Java threads were implemented using user threads, and Java threads were implemented based on the OS native threading model in JDK 1.2 and beyond.

The implementation of user thread, UT, means that the thread is built in the user state space, the creation, synchronization, scheduling and destruction of the thread are done in the user state, and the process is 1:n with the user thread. In this case, the kernel cannot know how many user threads there are, and the implementation is more complex.

Using kernel thread implementations refers to implementing threads based on lightweight processes (light Weight process, LWP). Each lightweight process has a kernel thread (kernel-level thread, KLT) support, and a 1:1 correspondence between kernel threads. In this case, the dispatch thread may need to switch between the kernel state and the user state. Because lightweight processes need to consume kernel resources, the number of threads that can be supported is limited.

As in Windows and Linux systems, the native threading model of the operating system is a 1:1 correspondence, and for Sun JDK a Java thread corresponds to a lightweight process.

Thread Scheduling and Status

In the Java thread scheduling method is preemptive scheduling, that is, the system to be responsible for the time allocation of the various threads, and the thread after the allocation of time to schedule the next threads. No single thread can monopolize the CPU. The Java language sets a total of 10 thread priorities, and when two threads are waiting to execute at the same time, the priority is first dispatched. The priority of a thread is mapped to the native thread of the operating system, but the priority of each operating system is not exactly the same, so two Java threads with different priority levels may also be at the same priority when executed in the operating system.

Java defines 5 thread states, namely new, run (Running), wait (waiting), wait (Timed waiting), Block (Blocked), and End (Terminated). At any one time, the thread is in one of 5 states and switches between States.

Each of these states has the following meanings:

NEW: Not started after creation;

Run: For Java, the thread is already running, but for the operating system it may be running or waiting;

Wait: The thread waits for the other thread to wake up, such as calling wait, join, and no time-out specified;

Deadline wait: The thread waits for a period of time after being awakened by the system, such as a call to sleep, wait, join, and set the timeout time;

Blocking: Threads entering the synchronization area need to be synchronized with other threads, such as the need to enter the synchronized zone but other threads have not exited the zone;

End: The thread ends after the Run method finishes executing.

Virtual Machine Stack

In the Java memory model, each virtual machine thread has its own private virtual machine stack. The stack is created at the same time as the thread, which stores the stack frame of the thread. Each method's invocation corresponds to a stack frame's stack and stack. In the stack frame, the local variable table (Variable table), the action stack (Operand stack), the dynamic connection (linking), the return address, and other additional information are stored.

The working memory of the thread

In the memory model, Java requires that all variables be stored in main memory, with each thread having its own working memory. The working memory holds a copy of the main memory of the variable that the thread needs to read and write. Thread-to-read operations on variables are performed directly in working memory and do not operate on the contents of main memory, and the synchronization of primary and working memory is done by the virtual machine. Different threads cannot access each other's working memory, and the transfer of the variable value needs to go through main memory to complete.

Volatile-Modified variables ensure that variables are visible to all threads, that is, after a thread modifies a variable, other threads can always read the new value immediately. Even so, the self-increment self-decrement operation of volatile variables does not guarantee thread safety when multithreading is concurrent.

Summarize

Threads can only be created in Java by creating an instance of the thread class. After JDK 1.2, threads in Java implement threads based on the native threading model of the operating system. A thread is dispatched in a preemptive manner, that is, the system is responsible for the time allocation of each thread, and the next thread is dispatched after the threads have used the allotted time. Java defines 5 thread states: New, run, wait, wait, block, and end.

Each virtual machine thread has its own private virtual machine stack. The stack is created at the same time as the thread, where the stack frame of the thread is stored. Each method's invocation corresponds to a stack frame's stack and stack. Each thread has its own working memory, and the working memory holds a copy of the main memory of the variable that the thread needs to read and write. Thread-to-read operations on variables are performed directly in working memory and do not operate on the contents of main memory, and the synchronization of primary and working memory is done by the virtual machine.

3 Weekly study notes or technical summaries for a basic Java programmer, including Java Advanced, virtual machine, MySQL, NoSQL, distributed computing, open source framework, and many other fields. Follow the author or public number backend-develop the first time to get the latest content.

Original address: Java Threading Knowledge Supplements

Java Threading Knowledge Supplements

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.