当前位置: 技术问答>java相关
线程的问题[求助]
来源: 互联网 发布时间:2015-10-06
本文导语: class NewThread implements Runnable { Thread t; String name; NewThread(String threadName) { name = threadName; t = new Thread(this, name); t.start(); } public void run() { try { for (int i=5;i>0 ;i-- ) { System.out.println(name + ":" +i...
class NewThread implements Runnable
{
Thread t;
String name;
NewThread(String threadName)
{
name = threadName;
t = new Thread(this, name);
t.start();
}
public void run()
{
try
{
for (int i=5;i>0 ;i-- )
{
System.out.println(name + ":" +i );
Thread.sleep(1000);
}
}catch(InterruptedException e)
{
System.out.println(name + "interrupted" );
}
System.out.println(name + "exiting !");
}
};
class ThreadTest
{
public static void main(String args[])
{
NewThread ob1 = new NewThread("one");
NewThread ob2 = new NewThread("two");
NewThread ob3 = new NewThread("Three");
System.out.println("Thread one is alive :" + ob1.t.isAlive());
System.out.println("Thread two is alive :" + ob2.t.isAlive());
System.out.println("Thread Three is alive :" + ob3.t.isAlive());
try
{
System.out.println("waiting for thread finnish");
ob1.t.join();
ob2.t.join();
ob3.t.join();
}catch(InterruptedException e)
{
System.out.println("Thread interrupted!");
}
}
};
上面的程序的输出结果是:
Thread one is alive :true
Thread two is alive :true
Thread Three is alive :true
waiting for thread finnish
one:5
two:5
Three:5
one:4
two:4
Three:4
one:3
two:3
Three:3
one:2
two:2
Three:2
one:1
two:1
Three:1
oneexiting !
twoexiting !
Threeexiting !
问题:
为什么输出不是:
waiting for thread finnish
one:5
two:5
Three:5
one:4
two:4
Three:4
one:3
two:3
Three:3
one:2
two:2
Three:2
one:1
two:1
Three:1
oneexiting !
twoexiting !
Threeexiting !
Thread one is alive :true
Thread two is alive :true
Thread Three is alive :true
{
Thread t;
String name;
NewThread(String threadName)
{
name = threadName;
t = new Thread(this, name);
t.start();
}
public void run()
{
try
{
for (int i=5;i>0 ;i-- )
{
System.out.println(name + ":" +i );
Thread.sleep(1000);
}
}catch(InterruptedException e)
{
System.out.println(name + "interrupted" );
}
System.out.println(name + "exiting !");
}
};
class ThreadTest
{
public static void main(String args[])
{
NewThread ob1 = new NewThread("one");
NewThread ob2 = new NewThread("two");
NewThread ob3 = new NewThread("Three");
System.out.println("Thread one is alive :" + ob1.t.isAlive());
System.out.println("Thread two is alive :" + ob2.t.isAlive());
System.out.println("Thread Three is alive :" + ob3.t.isAlive());
try
{
System.out.println("waiting for thread finnish");
ob1.t.join();
ob2.t.join();
ob3.t.join();
}catch(InterruptedException e)
{
System.out.println("Thread interrupted!");
}
}
};
上面的程序的输出结果是:
Thread one is alive :true
Thread two is alive :true
Thread Three is alive :true
waiting for thread finnish
one:5
two:5
Three:5
one:4
two:4
Three:4
one:3
two:3
Three:3
one:2
two:2
Three:2
one:1
two:1
Three:1
oneexiting !
twoexiting !
Threeexiting !
问题:
为什么输出不是:
waiting for thread finnish
one:5
two:5
Three:5
one:4
two:4
Three:4
one:3
two:3
Three:3
one:2
two:2
Three:2
one:1
two:1
Three:1
oneexiting !
twoexiting !
Threeexiting !
Thread one is alive :true
Thread two is alive :true
Thread Three is alive :true
|
其实对于只样的多线程的执行是有原因的,其实主要原因是系统把cpu分成若干的时间片,所以才回出现上面的情况。