您的位置:首页 > 其它

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();

}
}


测试打印结果
测试1construction this alive:false
construction Thread alive:true
main thread2 alive: true
run this alive:true
run Thread alive:true
测试2construction this alive:false
construction Thread alive:true
run this alive:true
run Thread alive:true
main thread2 alive: false
测试3construction this alive:false
construction Thread alive:true
run this alive:true
run Thread alive:true
测试4construction this alive:false
construction Thread alive:true
run this alive:false
run Thread alive:true
测试1和测试2对比,Thread.sleep(5000)之后thread2已经执行完毕了,所以不处于活动状态

测试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()取得线程的唯一标识.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: