The difference between Thread class run() and start() in Java

51CTO 2022-08-06 12:47:47 阅读数:25

differencethreadclassrunstart


转自:

 ​Java中Thread类run(),start()区别​

下文讲述Thread类中run(),start()The differences in the methods are explained,如下所示:

run(),start()method function difference:
1.start()方法用于启动线程,run方法只是ThreadA normal method of subclasses
2.当使用start()方法启动线程后,The thread will run in the classrun方法
  • 1.
  • 2.
  • 3.

例:

package com.java265.other;
public class Test6 {
public static void main(String[] args) throws Exception {
ThreadTest threadTest = new ThreadTest();
System.out.println("run方法运行!");
threadTest.run();
System.out.println("start方法运行");
threadTest.start();
}
}

class ThreadTest extends Thread {
public ThreadTest() {}
public ThreadTest(String name) {
super(name);
}

@Override
public void run() {
System.out.println("I am from the classThreadTest!");
}
}
-----运行以上代码,将输出以下信息-----
run方法运行!
I am from the classThreadTest!
start方法运行
I am from the classThreadTest!
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.


copyright:author[51CTO],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/218/202208061244350038.html