标签:target stack i++ sys 特征 public 并且 extends span
public class TestMitiThread { public static void main(String[] rags) { System.out.println(Thread.currentThread().getName() + " 线程运行开始!"); new MitiSay("A").start(); new MitiSay("B").start(); System.out.println(Thread.currentThread().getName() + " 线程运行结束!"); } } class MitiSay extends Thread { public MitiSay(String threadName) { super(threadName); } public void run() { System.out.println(getName() + " 线程运行开始!"); for (int i = 0; i < 10; i++) { System.out.println(i + " " + getName()); try { sleep((int) Math.random() * 10); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(getName() + " 线程运行结束!"); } }
运行结果: main 线程运行开始! main 线程运行结束! A 线程运行开始! 0 A 1 A B 线程运行开始! 2 A 0 B 3 A 4 A 1 B 5 A 6 A 7 A 8 A 9 A A 线程运行结束! 2 B 3 B 4 B 5 B 6 B 7 B 8 B 9 B B 线程运行结束!
public class TestMitiThread1 implements Runnable { public static void main(String[] args) { System.out.println(Thread.currentThread().getName() + " 线程运行开始!"); TestMitiThread1 test = new TestMitiThread1(); Thread thread1 = new Thread(test); Thread thread2 = new Thread(test); thread1.start(); thread2.start(); System.out.println(Thread.currentThread().getName() + " 线程运行结束!"); } public void run() { System.out.println(Thread.currentThread().getName() + " 线程运行开始!"); for (int i = 0; i < 10; i++) { System.out.println(i + " " + Thread.currentThread().getName()); try { Thread.sleep((int) Math.random() * 10); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + " 线程运行结束!"); } }
运行结果: main 线程运行开始! Thread-0 线程运行开始! main 线程运行结束! 0 Thread-0 Thread-1 线程运行开始! 0 Thread-1 1 Thread-1 1 Thread-0 2 Thread-0 2 Thread-1 3 Thread-0 3 Thread-1 4 Thread-0 4 Thread-1 5 Thread-0 6 Thread-0 5 Thread-1 7 Thread-0 8 Thread-0 6 Thread-1 9 Thread-0 7 Thread-1 Thread-0 线程运行结束! 8 Thread-1 9 Thread-1 Thread-1 线程运行结束!
标签:target stack i++ sys 特征 public 并且 extends span
原文地址:http://www.cnblogs.com/houjiie/p/6127233.html