调用线程的start方法是创建了新的线程,在新的线程中执行。
调用线程的run方法是在主线程中执行该方法,和调用普通方法一样
实例:
package com.test.interview;
public class ThreadDemo {
private static void attack() {
System.out.println("fight");
System.out.println("Attack current thread is :" + Thread.currentThread().getName());
}
public static void main(String[] args) {
Thread t = new Thread() {
@Override
public void run() {
attack();
}
};
System.out.println("current thread is :" + Thread.currentThread().getName());
t.start();
}
}
运行结果:
current thread is :main
fight
Attack current thread is :Thread-0