Home > Java > javaTutorial > body text

What is the difference between java start() and run()

WBOY
Release: 2023-04-25 08:43:06
forward
2185 people have browsed it

1. Concept difference

start(): After generating the thread object, call the start() method to start the thread. The thread is in the Ready state in the running state RUNNABLE. At this time The thread waits to be scheduled by the CPU, and then executes the run() method after scheduling, and uses the start() method to start the thread, truly realizing multi-threading.

run(): The run() method is a common method in Thread. If you call the run() method directly with the thread object, it will run in the main thread. Because there is only one main thread in the program, when there are two threads in the program, the run() method is called directly, and the program is executed in sequence, and multi-threading is not implemented.

2. Example

public static void main(String[] args) {
    Thread t1 = new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName());
        }
    },"unstoppbale_t");
    t1.start();
}
Copy after login

The above is the detailed content of What is the difference between java start() and run(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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