3.Thread中的静态方法
2015-11-18 10:55
323 查看
1.currentThread()
运行结果:
静态块的打印:main
构造方法的打印: main
run()方法的打印:Thread-0
这个例子说明了,线程的构造方法、静态块是被main线程调用的,而线程类的run()方法才是应用线程自己调用的。
运行结果:
当在执行Thread15 thread15 = new Thread15()的时候,this.getName也就是当前线程却是Thread-0.
2.sleep(long millions)
该方法的作用是在指定的时间内让当前正在执行的线程Thread.currentThread()暂停执行,也就是休眠。值得注意的一点是,该方法并不让出cpu资源,换句话说,也就是CPU依然在执行run()中的内容,无非这个内容是休眠而已。
执行结果:
System.out.print是静态方法,因此输出的内容很快就会完成。启动的线程中有个休眠过程,thread-0 begin 和 thread-0 end 中间就会有等待。
3.yield()
yield()就是暂停当前执行的线程,并执行其他线程。这个暂停会是放弃cpu资源的,并且放弃cpu时间不确定,有可能刚放弃就又获得了。因此这里因为yield()方法中间消耗的时间也是不确定的。
4.interrupted()
测试当前线程是否已经中断,执行后就将状态标示改为false。因此,两次调用后返回必然为false
执行结果:
public class Thread14 extends Thread{ static { System.out.println("静态块的打印:" + Thread.currentThread().getName()); } public Thread14() { System.out.println("构造方法的打印: " + Thread.currentThread().getName()); } public void run() { System.out.println("run()方法的打印:" + Thread.currentThread().getName()); } //test public static void main(String[] args) { Thread14 thread14 = new Thread14(); thread14.start(); } }
运行结果:
静态块的打印:main
构造方法的打印: main
run()方法的打印:Thread-0
这个例子说明了,线程的构造方法、静态块是被main线程调用的,而线程类的run()方法才是应用线程自己调用的。
public class Thread15 extends Thread{ public Thread15() { System.out.println("thread15 ---->begin"); System.out.println("Thread.currentThread().getName()----->" + Thread.currentThread().getName()); System.out.println("this.getName()----->" + this.getName()); System.out.println("thread15---->end"); } public void run() { System.out.println("run------->begin"); System.out.println("Thread.currentThread.getName()---->" + Thread.currentThread().getName()); System.out.println("this.getName()------>" + this.getName()); System.out.println("run------>end"); } public static void main(String[] args) { Thread15 thread15 = new Thread15(); thread15.start(); } }
运行结果:
thread15 ---->begin Thread.currentThread().getName()----->main this.getName()----->Thread-0 thread15---->end run------->begin Thread.currentThread.getName()---->Thread-0 this.getName()------>Thread-0 run------>end
当在执行Thread15 thread15 = new Thread15()的时候,this.getName也就是当前线程却是Thread-0.
2.sleep(long millions)
该方法的作用是在指定的时间内让当前正在执行的线程Thread.currentThread()暂停执行,也就是休眠。值得注意的一点是,该方法并不让出cpu资源,换句话说,也就是CPU依然在执行run()中的内容,无非这个内容是休眠而已。
public class Thread16 extends Thread{ public void run() { try{ System.out.println("run threadName= " + this.getName() + " begin"); Thread.sleep(20000); System.out.println("run threadName= " + this.getName() + " end"); }catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { Thread16 thread16 = new Thread16(); System.out.println("begin= " + System.currentTimeMillis()); thread16.start(); System.out.println("end= " + System.currentTimeMillis()); } }
执行结果:
begin= 1447813479735 end= 1447813479736 run threadName= Thread-0 begin run threadName= Thread-0 end
System.out.print是静态方法,因此输出的内容很快就会完成。启动的线程中有个休眠过程,thread-0 begin 和 thread-0 end 中间就会有等待。
3.yield()
public class Thread17 extends Thread{ public void run() { long beginTime = System.currentTimeMillis(); int count = 0; for(int i = 0; i < 50000000; i++) { Thread.yield();//去掉之后明显整个程序运行时间剪短 count = count + i + 1; } long endTime = System.currentTimeMillis(); System.out.println("用时:" + (endTime - beginTime) + "毫秒" + count); } public static void main(String[] args) { Thread17 thread17 = new Thread17(); thread17.start(); // Thread.currentThread().interrupt(); // System.out.println("1---" + Thread.interrupted()); // System.out.println("2---" + Thread.interrupted()); } }
yield()就是暂停当前执行的线程,并执行其他线程。这个暂停会是放弃cpu资源的,并且放弃cpu时间不确定,有可能刚放弃就又获得了。因此这里因为yield()方法中间消耗的时间也是不确定的。
4.interrupted()
public static void main(String[] args) { // Thread17 thread17 = new Thread17(); // thread17.start(); Thread.currentThread().interrupt(); System.out.println("1---" + Thread.interrupted()); System.out.println("2---" + Thread.interrupted()); }
测试当前线程是否已经中断,执行后就将状态标示改为false。因此,两次调用后返回必然为false
执行结果:
1---true 2---false
相关文章推荐
- FusionCharts属性
- 获取手机内部所有应用的方法比较 PackageInfo、ResolveInfo
- Spring MVC 中 HandlerInterceptorAdapter的使用
- 游戏中的任务系统的简单架构!
- 线程调度 http://blog.csdn.net/loyal_baby/article/details/4202083
- android View 总结
- Android Service 双进程常驻后台(2)
- HMM算法
- struts2单文件,多文件上传
- 菜鸟谈谈二维数组
- UITabView 添加
- sql优化的50中方法
- android项目——透明圆角框的制作
- SpringMVC学习系列(9) 之 实现注解式权限验证
- Redis学习笔记(三)
- 欢迎使用CSDN-markdown编辑器
- Android 4.4 以上分分钟实现状态栏伪沉浸(状态栏透明)
- Ubuntu14.04+CUDA6.5+OpenCV2.4+Caffee配置
- Android 从底层实现让应用杀不死(1)
- 这样想,你永远都加不了薪!