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

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!

Statement
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
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor