1.2线程常用方法

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()取得线程的唯一标识.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值