Java and threading

Source: Internet
Author: User

Implementation of Threads

1. Using kernel thread implementations

Directly using the kernel-supported threads of the operating system, which threads are threaded by the kernel, the kernel dispatches the threads through the manipulation Scheduler (Scheduler) and is responsible for mapping the threads ' tasks to each processor.

Programs typically do not go directly to kernel threads, but instead use a high-level interface of kernel threads----lightweight process (light wiehgt process, LWP), which is what we normally call threads. A lightweight process is a one-to-1:1 relationship with kernel threads.

Limitations:

1) due to the kernel thread implementation, various threading operations, such as creation, destruction, and synchronization, require system calls, which are costly and need to be switched back and forth between the user mode and the kernel state (Kernel mode);

2) Each lightweight process needs to have a kernel thread support, so the lightweight process consumes a certain amount of kernel resources, so the number of lightweight processes that a system supports is limited.

2. Using the user thread implementation

The system kernel is not aware of thread presence, but is completely built on the line libraries of the user control.

This thread does not need to switch to the kernel state, the operation can be very fast low consumption, also can support the large number of threads, some high-performance database multithreading is implemented by the user thread.

The 1:n relationship between this process and the user is called a one-to-many threading model.

Limitations:

1) thread creation switch scheduling all need to consider the problem, and because the operating system only the processor resources allocated to the process, blocking processing, multi-processor system in how to map threads to other processors such as such problems are very difficult to solve, and even refused to complete.

So fewer programs are using the user thread.

3. Using the user line loads lightweight process hybrid implementation

The user thread is still fully built into the user control, so operations such as creating and switching the user thread are still inexpensive and can support large-scale concurrency with the user thread.

The lightweight process supported by the operating system serves as a bridge between the user thread and the kernel thread, so that the kernel provides thread scheduling and processor mapping, and the user thread's system calls are done through lightweight threads, reducing the risk that the entire process will be completely blocked.

In this hybrid mode, the number of user threads versus lightweight processes is variable, n:m, and many-to-many threading models.

Implementation of Java Threads

The Java threading model is implemented based on the native threading model of the operating system, and the threading model supported by the operating system largely determines how the threads of a Java virtual machine are mapped.

For Sun JDK, both Windows and Linux are implemented using a one-to-one threading model. A Java thread is mapped to a lightweight process, because the threading model provided by Windows and Linux systems is single.

Java Thread Scheduling

1. Collaborative thread scheduling (cooperative threads-scheduling)

The execution time of a thread is controlled by the thread itself, and after the thread has executed its own work, it is proactive to notify the system to switch to another thread.

The downside is that The thread execution time is not controllable and the program may be stuck there.

2. Preemptive thread scheduling (preemptive threads-scheduling)

Each thread will have a system to allocate execution time, and the thread's switchover is not determined by the thread itself

Benefits: The execution time of a thread is system controllable, there is no problem that a thread causes the whole process to block, and the thread used by Java is scheduled in a preemptive manner.

Thread state transitions

Java and threading

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.