Home > Java > javaTutorial > How to implement threads in Java? Implementation method of Java thread (picture and text)

How to implement threads in Java? Implementation method of Java thread (picture and text)

不言
Release: 2018-09-17 15:18:24
Original
2084 people have browsed it

The content of this article is about how to implement threads in Java? The implementation method of Java threads (pictures and texts) has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Processes and Threads

In traditional operating systems, the core concept is "process", and a process is an abstraction of a running program.
The existence of processes makes "parallelism" possible. In an operating system, multiple processes are allowed to run, and these processes "seem" to be running at the same time.
If our computer is running multiple processes such as web browsers, email clients, instant messaging software such as QQ WeChat, etc., we feel that these processes are all running at the same time. Assume that this computer is equipped with multiple CPU or multi-core CPU, then this phenomenon of multiple processes in parallel may not be surprising at all. Each process can be assigned a separate CPU, thus achieving multi-process parallelism.
However, in fact, when the computer has only one CPU, it can also give humans the feeling that multiple processes are running at the same time. But human feelings are often vague and imprecise. The fact is that because the computing speed of the CPU is very fast, it can quickly switch between processes. At a certain moment, the CPU can only run one process, but within a second, it can quickly switch between processes, allowing people to Creates the illusion that multiple processes are running at the same time.
In the operating system, why is the concept of thread derived from the process?

  1. Because for some processes, multiple activities will occur inside it, some activities may block at a certain time, and some activities will not. If these activities are separated by threads If you enable them to run in parallel, it will be easier to design the program.

  2. Threads are more lightweight than process creation and consume less performance

  3. If a process requires both CPU computing and I/ O processing, having multiple threads allows these activities to overlap, speeding up the execution of the entire process.

Each process has an independent memory address space in the operating system. All threads created by the process share this memory. An operating system that supports multi-threading will allow threads to function as The smallest unit of CPU scheduling. CPU time slices are divided among different threads.

Possible implementation methods of threads

Basically, mainstream operating systems support threads and also provide thread implementations. In order to cope with the differences in different hardware and operating systems, the Java language provides a unified abstraction of thread operations. In Java, we use the Thread class to represent a thread.
The specific implementation of Thread may have different implementation methods:

Use kernel thread implementation

Kernel thread is a thread supported by the operating system kernel. There is a thread table in the kernel for Record all threads in the system. When creating or destroying a thread, system calls are required, and then the thread table is updated in the kernel. Blocking of kernel threads and other operations all involve system calls. System calls are relatively expensive and involve switching back and forth between user mode and kernel mode. In addition, there is a thread scheduler inside the kernel that determines which thread the CPU time slice should be allocated to.
Programs generally do not directly operate kernel threads, but use a high-level interface of kernel threads, lightweight processes. The relationship between lightweight processes and kernel threads is 1:1. Each lightweight process is supported by a kernel thread.

How to implement threads in Java? Implementation method of Java thread (picture and text)

In the above figure, LWP refers to Light Weight Process, which is lightweight process; KLT refers to Kernel Level Thread, which is kernel thread.

Use user threads to implement

User threads are thread libraries implemented by programs or programming languages. The system kernel cannot sense the existence of these threads. The establishment, synchronization, destruction and scheduling of user threads are all completed in user mode without the help of the kernel and no system calls. The advantage of this is that the operation of threads is very efficient. In this case, the ratio of processes to user threads is 1:N.

How to implement threads in Java? Implementation method of Java thread (picture and text)

User-mode threads will face difficulties when faced with how to block threads. Blocking a user-mode thread will block the entire process. Multithreading also loses its meaning. Due to the lack of kernel support, many tasks that require the use of the kernel, such as blocking and waking up threads, thread mapping in a multi-CPU environment, etc., require user programs to implement them, which can be extremely difficult to implement.

Use a mixed implementation of user threads and kernel threads

In this mixed implementation, there are both user threads and kernel threads. The operations of creating and switching user-mode threads are still very efficient, and threads implemented in user-mode make it easier to increase the size of threads. Functions that require support from the operating system kernel are implemented through kernel threads, such as mapping to different processors, processing thread blocking and waking up, and kernel thread scheduling. This implementation will still use lightweight process LWP, which is the bridge between user threads and kernel threads.

How to implement threads in Java? Implementation method of Java thread (picture and text)

Implementation of Java threads

Before JDK1.2, Java threads were implemented using user threads. In JDK1. 2. After that, Java was implemented using the threading model natively supported by the operating system. The implementation of the Java threading model mainly depends on the operating system. For Oracle JDK, the threading model on Windows and Linux is implemented in a one-to-one manner, that is, a Java thread is mapped to a lightweight process (kernel thread). In the Solaris platform, Java supports 1:1 and N:M thread models.

Thread blocking and waiting

The status of threads in Java is as follows: New, Runnable, Waiting, TimedWaiting, Blocked, Terminated.

How to implement threads in Java? Implementation method of Java thread (picture and text)

  • NEW: Thread is initially created, not running

  • RUNNABLE: Thread Running, but not necessarily consuming CPU

  • BLOCKED: The thread is waiting for another thread to release the lock

  • WAITING: The thread has executed wait, join , LockSupport.park() method

  • TIMED_WAITING: The thread called sleep, wait, join, LockSupport.parkNanos() and other methods. Different from the WAITING state, these methods have representation time parameters.

The important difference between Blocked and Waiting is that a thread in the blocked (Blocked) state is waiting to acquire an exclusive lock. For example, the thread is waiting to enter a critical section surrounded by the synchronized keyword. , it enters the Blocked state. The Waiting state is waiting to be awakened, or waiting for a period of time.

The above is the detailed content of How to implement threads in Java? Implementation method of Java thread (picture and text). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template