public class Test1 extends Thread {
@Override public void run() {
while (true)
{
System.out. println(Thread.currentThread().getName());
}
}
public static void main(String[] args) {
Test1 test1=new Test1();
test1.run(); //Output result main
test1.start(); //Output result Thread-0
}
}
1.start
start is to start a new thread.
When start() is used to start a thread, the thread enters the ready state, making the virtual processor represented by the thread in a runnable state, which means that it can be scheduled and executed by the JVM. But this does not mean that the thread will run immediately. Only when the CPU allocates a time slice and this thread obtains the time slice, does it start executing the run() method. start() cannot be called repeatedly, it calls the run() method. The run() method is what you must override
2.run
run() just Like ordinary member methods, they can be called repeatedly.
If you call the run method directly, a new thread will not be started! There is still only one thread in the program, the main thread, and there is still only one program execution path. It still needs to be executed sequentially, and it still needs to wait for the run method body to be executed before continuing to execute the following code. In this way, the purpose of multi-threading is not achieved.
Call the start method to start the thread, and the run method is just a normal method call of thread, which is still executed in the main thread.
The above is the detailed content of What is the difference between start and run in java thread. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function