多线程笔记(3)线程的常用方法之一


一、currentThread()方法

Thread.currentThread()方法可以获得当前线程。

Java 中的任何一段代码都是执行在某个线程当中的,执行当前代码的线程就是当前线程。

同一段代码可能被不同的线程执行, 因此当前线程是相对的,Thread.currentThread()方法的返回值是在代码实际运行时候的线程对象。

1.简单案例

package com.test.thread;

/**
 * 定义线程类
 * 分别在构造方法中和 run 方法中打印当前线程
 */
public class SubThread1 extends Thread{
    public SubThread1(){
        System.out.println("构 造 方 法 打 印 当 前 线 程 的 名 称 : " + Thread.currentThread().getName());
    }

    @Override
    public void run() {
        System.out.println("run 方 法 打 印 当 前 线 程 名 称 :" + Thread.currentThread().getName());
    }

}

package com.test.thread;

/**
 * 测试当前线程
 */
public class Test01CurrentThread {
    public static void main(String[] args) throws InterruptedException{
        System.out.println("main 方 法 中 打 印 当 前 线 程 :" + Thread.currentThread().getName());
        //创建子线程, 调用 SubThread1()构造方法, 在main线程中调用构造方法,所以构造方法中的当前线程就是main线程
        SubThread1 t1 = new SubThread1();
        t1.start(); //启动子线程,子线程会调用 run()方法,所以run()方法中的当前线程就是 Thread-0子线程
        Thread.sleep(5000); //main 线程睡眠 5000 毫秒
        t1.run(); //在 main方法中直接调用run()方法,没有开启新的线程,所以在 run方法中的当前线程就是 main线程
    }
}

main方法运行结果如下:

在这里插入图片描述
分析:主线程main先执行构造方法,子线程执行run方法,主线程main等待5秒后执行run方法。

2.复杂案例

package com.test.thread;

/**
 * 当前线程的复杂案例
 */
public class SubThread2 extends Thread{

    public SubThread2(){
        System.out.println("构 造 方 法 中 ,Thread.currentThread().getName() : " + Thread.currentThread().getName() );
        System.out.println("构造方法,this.getName() : " + this.getName());
    }

    @Override
    public void run() {
        System.out.println("run 方 法 中 ,Thread.currentThread().getName() : " + Thread.currentThread().getName() );
        System.out.println("run 方法,this.getName() : " + this.getName());
    }
}

package com.test.thread;

public class Test02CurrentThread {
    public static void main(String[] args) throws InterruptedException{
        //创建子线程对象
        SubThread2 t2 = new SubThread2();
        //设置线程的名称
        t2.setName("t2");
        t2.start(); //t2子线程执行run方法,this为run方法的所属对象,即为new的SubThread2

        Thread.sleep(5000); //main 线程睡眠 5000 毫秒

        // Thread(Runnable)构造方法形参是 Runnable 接口,调用时传递的实参是接口的实现类对象
        Thread t3 = new Thread(t2);
        t3.start(); //Thread-1子线程执行run方法,this为run方法的所属对象,即为t2
    }
}

main方法运行结果如下:

在这里插入图片描述
分析:主线程执行构造方法,构造方法的this对象为new的子线程;子线程执行run方法,run方法的this对象为子线程;Thread(Runnable)构造方法,新子线程执行run方法,run方法的this对象为Thread(Runnable)构造方法传的实参,即为t2。

二、setName()/getName()方法

thread.setName(线程名称), 设置线程名称。

thread.getName()返回线程名称。

通过设置线程名称,有助于程序调试,提高程序的可读性, 建议为每个线程都设置一个能够体现线程功能的名称。

三、isAlive()方法

thread.isAlive()判断当前线程是否处于活动状态。

活动状态就是线程已启动并且尚未终止

package com.test.thread;

public class SubThread3 extends Thread{
    @Override
    public void run() {
        //运行状态,true
        System.out.println("run 方法, isalive = " + this.isAlive());
    }

}

package com.test.thread;

public class Test1 {
    public static void main(String[] args) {
        SubThread3 t3 = new SubThread3();
        //false,在启动线程之前
        System.out.println("begin==" + t3.isAlive());
        t3.start();
        //结果不一定,打印这一行时,如果 t3 线程还没结束就返回 true, 如果 t3 线程已结束,返回 false
        System.out.println("end==" + t3.isAlive());
    }
}

main方法运行结果如下:

在这里插入图片描述

四、sleep()方法

Thread.sleep(millis); 让当前线程休眠指定的毫秒数。

当前线程是指 Thread.currentThread()返回的线程。

1.简单使用

package com.test.thread;

/**
 * 子线程休眠
 */
public class SubThread4 extends Thread{
    @Override
    public void run() {
        try {
            System.out.println("run, threadname=" + Thread.currentThread().getName() + " ,begin= " + System.currentTimeMillis());
            Thread.sleep(2000); //当前线程睡眠 2000 毫秒
            System.out.println("run, threadname=" + Thread.currentThread().getName() + " ,end= " + System.currentTimeMillis());
        } catch (InterruptedException e) {
            //在子线程的 run 方法中, 如果有受检异常(编译时异常)需要处理,只有选择捕获处理,不能抛出处理
            e.printStackTrace();
        }
    }
}

package com.test.thread;

public class Test2 {
    public static void main(String[] args) throws InterruptedException {
        SubThread4 t4 = new SubThread4();
        System.out.println("main__begin = " + System.currentTimeMillis());
        t4.start(); //开启新的线程
        Thread.sleep(1000); //当前线程睡眠 1000 毫秒
        t4.run(); //在 main 线程中调用实例方法 run(),没有开启新的线程
        System.out.println("main__end = " + System.currentTimeMillis());
    }
}

main方法运行结果如下:

在这里插入图片描述
分析:t4.start(); t4.run();主线程和子线程不分先后执行,但在中间加入sleep,即让子线程先执行,主线程等待1秒;子线程执行run方法,run方法中子线程休眠两秒,主线程执行run方法,run方法中主线程休眠两秒;随后子线程end,主线程end。

2.使用线程休眠完成一个简易的计时器

package com.test.thread;

/**
 * 使用线程休眠 Thread.sleep 完成一个简易的计时器
 */
public class SimpleTimer {
    public static void main(String[] args) {
        int remaining = 60; //从 60 秒开始计时
        // 读取 main 方法的参数
        if (args.length == 1){
            remaining = Integer.parseInt(args[0]);
        }
        while(true){
            System.out.println("Remaining: " + remaining);
            remaining--;
            if (remaining < 0 ){
                break;
            }
            try {
                Thread.sleep(1000);//线程休眠
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Done!!");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

*『饶』*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值