线程的休眠和强制运行

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线程的存在,并且了解到线程争抢之激烈。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值