Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between start and run in java threads

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "what is the difference between start and run in java thread". Xiaobian shows you the operation process through actual cases. The operation method is simple and fast and practical. I hope this article "what is the difference between start and run in java thread" can help you solve the problem.

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 results main

test1.start(); //Output Thread-0

}

}

1.start

Start starts a new thread.

When a thread is started with start(), the thread enters a ready state, making the virtual processor represented by the thread runnable, which means it can be scheduled and executed by the JVM. But this does not mean that the thread will run immediately. The run() method is executed only when the CPU allocates a time slice and the thread acquires a time slice. start() cannot be called repeatedly, it calls the run() method. The run() method is something you have to override.

2.run

Run() is called repeatedly just like a normal member method.

If you call the run method directly, it doesn't start a new thread! There is still only one thread in the program, and its program execution path is still only one, or it needs to be executed sequentially, or it needs to wait for the run method body to execute before continuing to execute the following code, so it does not achieve the purpose of multithreading.

The start method is called to start the thread, while the run method is just an ordinary method call to thread, still executed in the main thread.

About "java thread start and run what is the difference" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report