class MyThread implements Runnable
{
public void run()
{
for(int i=0; i<50; i++)
{
System.out.println(Thread.currentThread().getName() + "...present=====" +i);
}
}
}
public class Demo943
{
public static void main(String args[])
{
MyThread my = new MyThread();
Thread t = new Thread(my);
t.start();
for(int i=0; i<50; i++)
{
if(i>10)
{
try
{
t.join();
}
catch (Exception e)
{
}
}
System.out.println("Main...." + i);
}
}
}
这是未经休眠的运行结果:
Main....0
Thread-0...
Main....1
Thread-0...
Main....2
Thread-0...
Thread-0...
Main....3
Thread-0...
Main....4
Thread-0...
Main....5
Thread-0...
Main....6
Thread-0...
Main....7
Thread-0...
Main....8
Thread-0...
Main....9
Thread-0...
Main....10
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Thread-0...
Main....11
Main....12
Main....13
Main....14
Main....15
Main....16
Main....17
Main....18
Main....19
Main....20
Main....21
Main....22
Main....23
Main....24
Main....25
Main....26
Main....27
Main....28
Main....29
Main....30
Main....31
Main....32
Main....33
Main....34
Main....35
Main....36
Main....37
Main....38
Main....39
Main....40
Main....41
Main....42
Main....43
Main....44
Main....45
Main....46
Main....47
Main....48
Main....49
================================================================================
class MyThread implements Runnable
{public void run()
{
for(int i=0; i<50; i++)
{
try
{
Thread.sleep(500); //经过休眠处理
}
catch (Exception e)
{
}
System.out.println(Thread.currentThread().getName() + "...present=====" +i);
}
}
}
public class Demo943
{
public static void main(String args[])
{
MyThread my = new MyThread();
Thread t = new Thread(my);
t.start();
for(int i=0; i<50; i++)
{
if(i>10)
{
try
{
t.join();
}
catch (Exception e)
{
}
}
System.out.println("Main...." + i);
}
}
}
休眠处理的结果:
Main....0
Main....1
Main....2
Main....3
Main....4
Main....5
Main....6
Main....7
Main....8
Main....9
Main....10
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Thread-0..
Main....11
Main....12
Main....13
Main....14
Main....15
Main....16
Main....17
Main....18
Main....19
Main....20
Main....21
Main....22
Main....23
Main....24
Main....25
Main....26
Main....27
Main....28
Main....29
Main....30
Main....31
Main....32
Main....33
Main....34
Main....35
Main....36
Main....37
Main....38
Main....39
Main....40
Main....41
Main....42
Main....43
Main....44
Main....45
Main....46
Main....47
Main....48
Main....49
对比可以发现,未经过休眠处理的线程,会跟Main线程争抢运行;而经过休眠处理之后,Main会趁其休眠的时间抢先运行。
这对于已入门Java的人来说虽然不是什么,但是通过这个小实验,清楚了Main线程的存在,并且了解到线程争抢之激烈。。。。