Java and threads

This article covers Java and threads from the following aspects:

 

1. Thread implementation

Second, Java thread scheduling

3. State transition

 

1. Thread implementation

        A thread is a lighter-weight scheduling execution unit than a process. The introduction of a thread can separate the resource allocation and scheduling of a process, and each thread can share resources and schedule independently;

        Thread is the basic unit of CPU scheduling;

 

1, the implementation of the thread

      Here, not the implementation of java threads.

      Three thread implementation methods: kernel thread implementation, user thread implementation, and user thread plus lightweight process hybrid implementation       

 

 (1) Kernel thread (KLT)

          A thread supported by the operating system kernel. This thread is switched by the kernel. The kernel schedules the thread through the operation scheduler, and is responsible for mapping the task of the thread to each processor.

          The program does not directly use the kernel thread, but uses a high-level interface of the kernel thread--Light weight process (LWP). The light weight process is the thread in the usual sense. .

 

(2) User thread (UT)

          On a fully built user space thread library, the system kernel cannot perceive the existence of threads. The establishment, synchronization, destruction, and scheduling of threads are completely completed in user mode without kernel participation.

          The program is properly implemented and does not need to switch with the kernel thread, which is highly efficient and low-consumption.

          Disadvantages: Without the guarantee of kernel threads, the operating system allocates resources to processes, such as: "how to deal with blocking", "how to map threads to other processors in a multi-processor system" It is extremely difficult to solve such problems.

 

(3) Mixed threads

         User threads are implemented alongside kernel threads.

         The operating system supports lightweight processes as a bridge between kernel threads and user threads. The kernel provides thread scheduling and processor mapping functions. The system calls of user threads are completed through lightweight threads, reducing the risk of the process being blocked.

 

2. Implementation of java thread

     Before JDK1.2, the user thread implementation named "green thread" was implemented, and after JDK1.2, it was implemented based on the native thread of the operating system.

 

Second, Java thread scheduling

       There are two scheduling methods: cooperative thread scheduling and preemptive thread scheduling

 

1. Cooperative thread scheduling

     advantage:

              Thread execution time is controlled by the thread itself;

              The implementation is simple, the thread executes its own thread task before switching to another thread, and the switching operation can be known by itself;

     shortcoming:

              Thread execution time is uncontrollable. If there is a problem with a thread and the system is not notified in time to switch, the program will be blocked;

 

2. Preemptive thread scheduling

     advantage: 

              Each thread is allocated execution time by the system, and thread switching is not determined by the thread itself;

 

3. State transition

        The java language defines 6 states of the process:

        New, Runable, Waiting, Timed Waiting, Blocked, Terminated



 

            

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326184708&siteId=291194637