日期:2014-05-20 浏览次数:20693 次
public class Test extends Thread { public static volatile int n = 0; public void run() { for (int i = 0; i < 10; i++, n++) try { sleep(1); // 为了使运行结果更随机,延迟3毫秒 } catch (Exception e) { } } public static void main(String[] args) throws Exception { Thread threads[] = new Thread[100]; for (int i = 0; i < threads.length; i++) // 建立100个线程 threads[i] = new Test(); for (int j = 0; j < threads.length; j++) // 运行刚才建立的100个线程 { threads[j].start(); threads[j].join(); } System.out.println("n=" + Test.n); } }
public class Test extends Thread { public static volatile int n = 0; public void run() { for (int i = 0; i < 10; i++, n++) try { sleep(1); // 为了使运行结果更随机,延迟3毫秒 } catch (Exception e) { } } public static void main(String[] args) throws Exception { Thread threads[] = new Thread[100]; for (int i = 0; i < threads.length; i++) // 建立100个线程 threads[i] = new Test(); for (int j = 0; j < threads.length; j++) // 运行刚才建立的100个线程 { threads[j].start(); } for (int k = 0; k < threads.length; k++) // 100个线程都执行完后继续 threads[k].join(); System.out.println("n=" + Test.n); } }