、public class Multithreading {
public static void main(String[] args)
{
Test he=new Test();
new Thread(he).start() ;
new Thread(he).start() ;
new Thread(he).start() ;
new Thread(he).start() ;
new Thread(he).start() ;
new Thread(he).start() ;
new Thread(he).start() ;
new Thread(he).start() ;
}
}
class Test implements Runnable
{
int top=1;
public void run()
{
while(top>0)
{
try{
Thread.sleep(1);
}catch(Exception e)
{}
System.out.println(Thread.currentThread().getName().toString()+“is running:top=“+top--);
}
}
}
为什么当top取值为1时,线程按顺序执行,top取其他值时,没有这种现象? |
|