动力节点首页 全国咨询热线:400-8080-105

绑定手机号,登录
手机号

验证码

微信登录
手机号登录
手机号

验证码

微信登录与注册
微信扫码登录与注册

扫码关注微信公众号完成登录与注册
手机号登录
首页 > 文章

5种Thread类方法

05-20 15:43 744浏览
举报 T字号
  • 大字
  • 中字
  • 小字

Thread类是java.lang包下的类,是多线程经常需要使用的类。在thread类中包含了很多Java多线程需要用到的方法,下面我们就来介绍5个比较重要的Thread类方法。

1.currentThread()方法

currentThread()方法可以返回代码被那个线程调用的信息。

测试方法如下:

public class MyThread extends Thread{
    public MyThread(){
        super();
    }
    public MyThread(String name){
        super();
        this.setName(name);
        System.out.println("构造器中线程名字:" + Thread.currentThread().getName());
    }

    @Override
    public void run() {
        super.run();
        System.out.println("this is MyThread");
        System.out.println("run方法中线程名字:" + Thread.currentThread().getName());
    }


    public static void main(String[] args){

        // 继承Thread
        MyThread myThread = new MyThread("myThread-name");
        myThread.start();
    }
}

输出内容:

构造器中线程名字:main
this is MyThread
run方法中线程名字:myThread-name

2.isAlive()方法

判断当前线程是否处于活跃状态,活跃状态是指线程已经启动并且尚未终止。

测试代码:

public class IsAliveFunc extends Thread {

    @Override
    public void run() {
        super.run();
        System.out.println("run is alive = " + this.isAlive());
    }


    public static void main(String[] args){
        IsAliveFunc isAliveFunc = new IsAliveFunc();
        System.out.println("begin start " + isAliveFunc.isAlive());
        isAliveFunc.start();
        System.out.println("after start " + isAliveFunc.isAlive());
    }

}

输出结果:

begin start false
after start true
run is alive = true

3.sleep()方法

sleep()方法是让正在运行的线程在指定毫秒数内暂停。

测试代码:

public class SleepFunc {

    public static void sleep(){
        System.out.println("begin");
        try {
            Thread.sleep(2000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("end");
    }

    public static void main(String[] args){
        System.out.println(" begin time = " + System.currentTimeMillis());
        sleep();
        System.out.println(" end time = " + System.currentTimeMillis());
    }
}

输出结果:

begin time = 1551138333348
begin
end
end time = 1551138335348

可以看出时间相差2秒。

4.join()方法

public final synchronized void join(long millis)

这个方法一般在其他线程中进行调用,指明当前线程需要阻塞在当前位置,等待目标线程所有指令全部执行完毕。例如:

Thread thread = new MyThreadT();
thread.start();
thread.join();

System.out.println("i am the main thread");

正常情况下,主函数的打印语句会在 MyThreadT 线程 run 方法执行前执行,而 join 语句则指明 main 线程必须阻塞直到 MyThreadT 执行结束。

5. interrupt()方法

interrupt()方法的使用并不像for+break语句那样,马上就停止循环。调用interrupt()方法仅仅是在当前线程中打了一个停止的标记,并不是真的停止线程。所以引出this.interrupted()和this.isInterrupted()方法。

1)this.interrupted()是判断当前线程是否已经是中断状态,执行此方法后具有将线程的中断状态标志清除为false的功能;

2)this.isInterrupted()是判断当前线程是否已经是中断状态,但是不清除状态标志。

所以使用interrupt()时需要判断线程是否有中断标志,在使用return或者抛异常的方式中断此线程。

thread类中的方法在Java多线程中得到了广泛的应用,还有很多的方法我们没有介绍到,感兴趣的小伙伴可以自己查阅资料或者直接在动力节点在线的视频课程中免费学习。
 

0人推荐
共同学习,写下你的评论
0条评论
代码小兵498
程序员代码小兵498

153篇文章贡献528999字

相关课程 更多>

作者相关文章更多>

推荐相关文章更多>

Java面试题及答案整理

提枪策马乘胜追击04-21 20:01

Spring常见面试题

代码小兵92504-17 16:07

Java零基础实战项目——五子棋

代码小兵98804-25 13:57

Java string类详解

杨晶珍05-11 14:54

6道经典算法面试题

杨晶珍05-12 16:39

发评论

举报

0/150

取消