Home > Java > JavaBase > body text

What is a thread in java? How to understand?

王林
Release: 2019-11-13 10:12:59
Original
3231 people have browsed it

What is a thread in java? How to understand?

The concept and principle of threads

A thread refers to an execution process in a process, and multiple threads can run in a process. For example, many threads can run in the java.exe process. A thread always belongs to a certain process. A thread does not have its own virtual address space and shares all resources allocated to the process with other threads in the process.

Threads are different from processes during execution. Each independent thread has an entry point for program execution, a sequential execution sequence, and an exit point for the program. However, threads cannot execute independently and must exist in the application program, and the application program provides multiple thread execution control.

Thread is an entity of the process and the basic unit of CPU scheduling and dispatch. It is a basic unit that is smaller than the process and can run independently. The thread itself basically does not own system resources, only a few resources that are essential for running (such as a program counter, a set of registers and a stack), but it can share all the resources owned by the process with other threads belonging to the same process. .

Threads have their own stack and local variables, but there is no separate address space between threads. A thread contains the following:

1. A pointer to the currently executed The instruction pointer of the instruction;

2. A stack;

3. A collection of register values ​​that defines a part of the value that describes the processor state of the executing thread;

4. A private data area.

Threads in Java

In Java, "thread" refers to two different things:

1. java.lang.Thread class An instance of;

2. Thread execution.

In Java programs, there are two ways to create threads:

First, derive the Thread class and override the run method;

Second, implement the Runnable interface create.

Recommended tutorial: Java tutorial

The above is the detailed content of What is a thread in java? How to understand?. 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