实例 Java 通过实现 Runnable 接口来运行线程

x
 
public class Main implements Runnable {
  public static void main(String[] args) {
    Main obj = new Main();
    Thread thread = new Thread(obj);
    thread.start();
    System.out.println("This code is outside of the thread");
  }
  public void run() {
    System.out.println("This code is running in a thread");
  }
}
                    

输出结果

This code is outside of the thread
This code is running in a thread