public class TestThread { public static void main(String[] args) throws InterruptedException { //启动了单线程 //Thread.sleep(100);//休眠,毫秒 /*for(int i = 0; i < 20; i++) { Thread.sleep(1000);//休眠,毫秒 System.out.println("i = " + i); }*/ ThreadTest tt = new ThreadTest(); tt.setName("zxc"); tt.start();//启动线程 ThreadTest tt1 = new ThreadTest(); tt1.setName("tt1"); tt1.start(); } }
public class ThreadTest extends Thread { public void run() { System.out.println("0.0"); for(int i = 0; i < 20; i++) { try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); }//休眠,毫秒 System.out.println(this.getName() + " i = " + i); } } }