本帖最后由 程金 于 2012-10-2 12:10 编辑
package test;
public class Test extends Thread
{
public static volatile int n = 0;
public void run()
{
for (int i = 0; i < 10; i++, n++)
try
{
sleep(3); // 为了使运行结果更随机,延迟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 = new Test();
for (int i = 0; i < threads.length; i++) // 运行刚才建立的100个线程
threads.start();
if (args.length > 0)
for (int i = 0; i < threads.length; i++) // 100个线程都执行完后继续
threads.join();
//不是说join的线程都执行完了,才向下执行吗,为什么结果n输出的都小于1000
System.out.println("n=" + Test.n);
}
} |