博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
线程的休眠和强制运行
阅读量:5263 次
发布时间:2019-06-14

本文共 3408 字,大约阅读时间需要 11 分钟。

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

转载于:https://www.cnblogs.com/tanjianwen/p/5245427.html

你可能感兴趣的文章
ArcGIS Engine 中的绘制与编辑
查看>>
Oracle--通配符、Escape转义字符、模糊查询语句
查看>>
c# 文件笔记
查看>>
第一页 - 工具的使用(webstorm)
查看>>
Linux 进程资源用量监控和按用户设置进程限制
查看>>
IE浏览器整页截屏程序(二)
查看>>
D3.js 之 d3-shap 简介(转)
查看>>
制作满天星空
查看>>
类和结构
查看>>
CSS3选择器(二)之属性选择器
查看>>
adidas crazylight 2018 performance analysis review
查看>>
typeset shell 用法
查看>>
python 之 循环语句
查看>>
心得25--JDK新特性9-泛型1-加深介绍
查看>>
[转]ceph网络通信模块_以monitor模块为例
查看>>
HDOJ 1754 I Hate It(线段树基本操作)
查看>>
latex tree
查看>>
安装NVIDIA驱动时禁用自带nouveau驱动
查看>>
HDU-1255 覆盖的面积 (扫描线)
查看>>
css3学习01
查看>>