线程的常用方法 - morris131/morris-book GitHub Wiki

##线程的常用方法

  1. currentThread()
  2. isAlive()
  3. sleep()

currentThread()

currentThread()方法可返回代码段正在被哪个线程调用的信息。

package com.morris.ch1;

public class CurrentThreadTest extends Thread{

    public CurrentThreadTest() {
        System.out.println("[CurrentThreadTest]Thread.currentThread().getName():" + Thread.currentThread().getName());
        System.out.println("[CurrentThreadTest]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());
    }

    public static void main(String[] args) {
        CurrentThreadTest thread = new CurrentThreadTest();
        thread.start();

    }
}

运行结果如下:

[CurrentThreadTest]Thread.currentThread().getName():main
[CurrentThreadTest]this.getName:Thread-0
[run]Thread.currentThread().getName():Thread-0
[run]this.getName:Thread-0

isAlive()

方法isAlive()功能是判断当前线程是否处于活动状态。活动状态就是线程启动且尚未终止,比如正在运行或准备开始运行。 线程处于就绪、运行、阻塞状态,方法isAlive()返回true。

package com.morris.ch1;

public class IsAliveTest extends Thread {

    public IsAliveTest() {
        System.out.println("[CurrentThreadTest]Thread.currentThread():" + Thread.currentThread().getName() + " " + Thread.currentThread().isAlive());
        System.out.println("[CurrentThreadTest]this:" + this.getName() + " " + this.isAlive());
    }

    @Override
    public void run() {
        System.out.println("[run]Thread.currentThread():" + Thread.currentThread().getName() + " " + Thread.currentThread().isAlive());
        System.out.println("[run]this:" + this.getName() + " " + this.isAlive());
    }

    public static void main(String[] args) throws InterruptedException {
        IsAliveTest c = new IsAliveTest();
        Thread thread = new Thread(c);
        System.out.println("start begin: " + thread.isAlive());
        thread.start();
        System.out.println("start end:" + thread.isAlive());
        Thread.sleep(1000);
        System.out.println("after sleep 1000:" + thread.isAlive());
    }
}

运行结果如下:

[CurrentThreadTest]Thread.currentThread():main true
[CurrentThreadTest]this:Thread-0 false
start begin: false
start end:true
[run]Thread.currentThread():Thread-1 true
[run]this:Thread-0 false
after sleep 1000:false

###sleep() sleep()方法用于将当前线程休眠一定时间(时间单位是毫秒),休眠的时间可以用于让其他线程完成当前工作,亦可以减少CPU占用时间。避免程序出现长时间CPU占用100%的情况。

package com.morris.ch1;

public class SleepTest extends Thread {

    @Override
    public void run() {
        System.out.println("run beigin:" + System.currentTimeMillis());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("run end:" + System.currentTimeMillis());
    }

    public static void main(String[] args) {
        SleepTest thread = new SleepTest();
        thread.start();
    }
}

运行结果如下:

run beigin:1528717851061
run end:1528717853062
⚠️ **GitHub.com Fallback** ⚠️