Home  >  Article  >  Java  >  What is a thread in java

What is a thread in java

王林
王林Original
2019-12-04 16:03:143498browse

What is a thread in java

What is a thread

A thread refers to an execution process in a process. Multiple processes can run in one process. thread. For example, many threads can run in the java.exe process. Threads always belong to a process, and multiple threads in a process share the memory of the process.

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

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

2. The execution of a thread.

Recommended java related video tutorials: java video tutorial

Use the java.lang.Thread class or java.lang.RunnableInterface Write code to define, instantiate and start new threads.

A Thread class instance is just an object, like any other object in Java, has variables and methods, lives and dies on the heap.

In Java, each thread has a call stack. Even if no new threads are created in the program, the thread is still running in the background.

A Java application always starts running from the main() method. The main() method runs in a thread, which is called the main thread.

Once a new thread is created, a new call stack is generated.

Threads are generally divided into two categories: user threads and waiting threads.

When all user threads finish executing, the JVM automatically shuts down. However, the waiting thread is not independent of the JVM. The waiting thread is generally created by the operating system or the user.

Java thread: creation and startup

1. Define thread

1. Extend java .lang.Thread class.

There is a run() method in this class, and you should pay attention to its usage:

public void run()

If the thread is constructed using an independent Runnable running object, call the run method of the Runnable object; Otherwise, the method does nothing and returns.

Subclasses of Thread should override this method.

2. Implement the java.lang.Runnable interface.

void run()

When you create a thread using an object that implements the interface Runnable, starting the thread will cause the object's run method to be called in an independently executed thread.

The general contract of the method run is that it may perform any desired operation.

2. Instantiate threads

1. If it is a thread that extends the java.lang.Thread class, just use new.

2. If it is a class that implements the java.lang.Runnable interface, use the Thread constructor:

Thread(Runnable target)
Thread(Runnable target, String name)
Thread(ThreadGroup group, Runnable target)
Thread(ThreadGroup group, Runnable target, String name)
Thread(ThreadGroup group, Runnable target, String name, long stackSize)

3. Start the thread

Call the start() method on the thread's Thread object instead of run() or other methods.

Before calling the start() method: the thread is in a new state. The new state means that there is a Thread object, but there is not yet a real thread.

After calling the start() method: a series of complex things happened

Start a new execution thread (with a new call stack);

The thread starts from the new state Transfer to a runnable state;

When the thread gets a chance to execute, its target run() method will run.

Note: There is nothing special about the run() method for Java. Like the main() method, it's just the method name (and signature) that the new thread knows to call. Therefore, it is legal to call the run method on Runnable or Thread. But it does not start a new thread.

Recommended related articles and tutorials: Zero Basic Introduction to Java

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