标签:style blog java color art for
1 public class ThreadA extends Thread { 2 private static int threadID = 0; 3 4 public ThreadA() { 5 super("ThreadID:" + (++threadID)); 6 } 7 8 public void run() { 9 try { 10 System.out.println(getName() + " 线程运行开始!"); 11 for (int i = 0; i < 10; i++) { 12 System.out.println(this.getName() + " is " + i); 13 14 Thread.sleep(1);//这里要sleep,不然看不出多线程的效果 15 } 16 } catch (Exception e) { 17 e.printStackTrace(); 18 } 19 } 20 21 }
public class MyTest { public static void main(String[] args){ ThreadA t1=new ThreadA(); ThreadA t2=new ThreadA(); t1.start(); t2.start(); } }
输入结果:
ThreadID:1 线程运行开始!
ThreadID:1 is 0
ThreadID:2 线程运行开始!
ThreadID:2 is 0
ThreadID:1 is 1
ThreadID:2 is 1
ThreadID:1 is 2
ThreadID:2 is 2
ThreadID:1 is 3
ThreadID:2 is 3
ThreadID:1 is 4
ThreadID:2 is 4
ThreadID:1 is 5
ThreadID:2 is 5
ThreadID:1 is 6
ThreadID:2 is 6
ThreadID:1 is 7
ThreadID:2 is 7
ThreadID:1 is 8
ThreadID:2 is 8
ThreadID:1 is 9
ThreadID:2 is 9
标签:style blog java color art for
原文地址:http://www.cnblogs.com/csu_xajy/p/3845250.html