Thread.run方法是同步方法
Thread.run方法是同步方法,
线程:
package com.stono.thread.page005; public class MyThread extends Thread { @Override public void run() { super.run(); System.out.println("MyThread"); } }
调用:
package com.stono.thread.page005; public class Run { public static void main(String[] args) { MyThread myThread = new MyThread(); // myThread.start(); // start是异步的,通常会在main的打印之后执行 // myThread.start(); // 多次调用start会有IllegalThreadStateException异常 myThread.run(); // run是同步方法,会在main的打印之前执行 System.out.println("运行结束!"); } }
修改线程类:
package com.stono.thread.page005; public class MyThread extends Thread { @Override public void run() { super.run(); System.out.println("MyThread:"+this.currentThread().getName()); } }
发现run方法其实是main Thread调用的,所以就是同步的方法;