public class MyThread extends Thread { public void run() { for (int i = 0; i < 100; i++) { System.out.println("MyThread " + i); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub MyThread thread = new MyThread(); thread.start(); for (int i = 0; i < 100; i++) { System.out.println("main " + i); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
运行结果
main 0 MyThread 0 main 1 MyThread 1 main 2 MyThread 2 MyThread 3 main 3 main 4 MyThread 4 main 5 MyThread 5
原文:https://www.cnblogs.com/zhangxuechao/p/13616694.html