Home  >  Article  >  Java  >  What is the difference between java start() and run()

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

WBOY
WBOYforward
2023-04-25 08:43:062151browse

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();
}

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete