1.2线程常用方法
2016-01-12 17:38
405 查看
currentThread()
可以返回代码段正在被哪个线程调用package com.myThread; public class Thread1 extends Thread { public Thread1() { System.out.println("construction this:" + this.currentThread().getName()); System.out.println("construction Thread:" + Thread.currentThread().getName()); } @Override public void run() { super.run(); System.out.println("run this:" + this.currentThread().getName()); System.out.println("run Thread:" + Thread.currentThread().getName()); } }
package com.test; import com.myThread.Thread1; public class Test1 { public static void main(String[] args) { //测试1 System.out.println(Thread.currentThread().getName()); //测试2 Thread1 thread1=new Thread1(); thread1.setName("thread1"); thread1.start(); //测试3 Thread1 thread1=new Thread1(); thread1.setName("thread1"); Thread thread =new Thread(thread1); thread.setName("thread"); thread.start(); } }
测试2打印结果
construction this:Thread-0 construction Thread:main run this:thread1//当前类对象Thread因为构造函数执行完了,分配了内存空间还setNam为“thread1” run Thread:thread1//当前线程对象,即调用start()方法的对象,这里是Thread1
测试3打印结果
construction this:Thread-0 construction Thread:main run this:thread1//this.getName取得当前类对象Thread1的名字“thread1”, run Thread:thread//当前线程对象,即调用start()方法的对象,这里是Thread
造成这样的结果的原因是:currentThread()方法返回的是当前线程对象,this代表的是当前类对象。
isAlive()
isAlive()测试线程是否处于活动状态,活动状态就是已经启动尚未停止package com.myThread; public class Thread2 extends Thread { public Thread2() { System.out.println("construction this alive:" + this.isAlive()); System.out.println("construction Thread alive:" + Thread.currentThread().isAlive()); } @Override public void run() { if(!"thread2".equals(Thread.currentThread().getName())){ super.run(); System.out.println("run this alive:" + this.isAlive()); System.out.println("run Thread alive:" + Thread.currentThread().isAlive());} else { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
package com.test; import com.myThread.Thread2; public class Test2 { public static void main(String[] args) throws InterruptedException { //测试1 // Thread2 thread2=new Thread2(); // thread2.setName("thread1"); // thread2.start(); // System.out.println("main thread2 alive: "+thread2.isAlive()); //测试2 // Thread2 thread2=new Thread2(); // thread2.setName("thread1"); // thread2.start(); // Thread.sleep(5000); // System.out.println("main thread2 alive: "+thread2.isAlive()); //测试3 // Thread2 thread2=new Thread2(); // thread2.setName("thread2"); // thread2.start(); // Thread thread =new Thread(thread2); // thread.setName("thread"); // thread.start(); } }
测试 | 打印结果 |
---|---|
测试1 | construction this alive:false construction Thread alive:true main thread2 alive: true run this alive:true run Thread alive:true |
测试2 | construction this alive:false construction Thread alive:true run this alive:true run Thread alive:true main thread2 alive: false |
测试3 | construction this alive:false construction Thread alive:true run this alive:true run Thread alive:true |
测试4 | construction this alive:false construction Thread alive:true run this alive:false run Thread alive:true |
测试3和测试4对比,Thread2对象里的run()方法已经设置Thread.sleep(5000),目的让Thread2的实现处于活动状态,所以在当前类用this.isAlive,只要当前类的实现启动线程,则为true如测试3一样
总结:star()方法启动,run()方法结束前是isAlive()为true
sleeps()
sleep(long millis)方法让当前“正在执行”的线程休眠(暂停)millis毫秒,正在执行指的是this.currentThread().getId()
getId()取得线程的唯一标识.相关文章推荐
- C++编程中的或||、与&&、非!逻辑运算符基本用法整理
- HDFS启动过程
- 插入排序
- tcpdump捕包乱序的解决方案
- android的动画相关参数说明
- 浏览器开发工具的25个秘密
- C# Best Practices - Define Fields Appropriately
- Android 实用工具Hierarchy Viewer实战
- bin nb0的区别介绍
- 自动交互脚本之expect
- android 在Button 的onDraw中加上invalidate()会导致背景缩放失败
- zabbix web检测数据库解析
- 为Linux 保留一部分物理内存
- ASP.NET 操作Cookie详解 增加,修改,删除
- COM组件注册真相
- 标准差
- ReactJs + BootStrap + Pager 分页
- Terracotta设计原理
- 什么叫集群,集群服务的工作原理?
- 集群、负载均衡的概念及其关系