Java多线程学习笔记(四)——Thread类中方法介绍

currentThread():返回代码正在被哪个线程调用。

public class CurrentThreadWay {
    
    public static void main(String[] args) {
        ThreadTest t = new ThreadTest();
        t.start();
    }
}
public class ThreadTest extends Thread{

    public ThreadTest(){
        System.out.println("调用构造方法的线程是:"+Thread.currentThread().getName());
    }
    public void run(){
        System.out.println("调用run方法的线程是:"+Thread.currentThread().getName());
    }
}

运行结果:

调用构造方法的线程是:main
调用run方法的线程是:Thread-0

isAlive():判断当前线程是否处于活动状态。活动状态就是线程已经启动尚未终止,线程处于正在运行或者开始准备运行状态。

public class IsAliveTest {
    public static void main(String[] args) {
        ThreadTest t = new ThreadTest();
        System.out.println("t在活动状态吗?="+t.isAlive());
        t.start();
        System.out.println("t在活动状态吗?="+t.isAlive());
    }
}
public class ThreadTest extends Thread{
    public void run(){
        System.out.println("run="+this.isAlive());
    }
}

运行结果:

t在活动状态吗?=false
t在活动状态吗?=true
run=true

sleep():是在指定毫秒内让当前"正在运行的线程(currentThread()返回的方法)"休眠(暂停执行)

public class SleepTest {
    public static void main(String[] args) {
        ThreadTest t = new ThreadTest();
        System.out.println("begin="+System.currentTimeMillis());//当前时间
        t.run();
        System.out.println("end="+System.currentTimeMillis());//休眠后时间
    }
}
public class ThreadTest extends Thread{
    public void run(){
        System.out.println("run threadName:"+this.currentThread().getName()+" begin...");
        try {
            Thread.sleep(2000);
            System.out.println("run threadName:"+this.currentThread().getName()+" end...");
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        
    }
}

运行结果:

begin=1492568214427
run threadName:main begin...
run threadName:main end...
end=1492568216428

修改上面代码:

public class SleepTest {
    public static void main(String[] args) {
        ThreadTest t = new ThreadTest();
        System.out.println("begin="+System.currentTimeMillis());
        t.start();
        System.out.println("end="+System.currentTimeMillis());
    }
}
public class ThreadTest extends Thread{
    public void run(){
        System.out.println("run threadName:"+this.currentThread().getName()+" begin= "+System.currentTimeMillis());
        try {
            Thread.sleep(2000);
            System.out.println("run threadName:"+this.currentThread().getName()+" end= "+System.currentTimeMillis());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        
    }
}

运行结果:

begin=1492568593345
end=1492568593345
run threadName:Thread-0 begin= 1492568593345
run threadName:Thread-0 end= 1492568595346

因为main线程和ThreadTest线程是异步执行的,所以首先先打印main线程的begin和end,在运行ThereadTest,打印run begin和run end相关信息。

getId():获取当前线程的唯一标识符

public class CurrentThreadWay {
    
    public static void main(String[] args) {
       ThreadTest t1 = new ThreadTest();
    ThreadTest t1 = new ThreadTest();
    System.out.println(Thread.currentThread().getName()+" "+Thread.currentThread().getId()); 
    t1.start();
    t2.start();
    System.out.println(t.getName()
+" "+t1.getId());
    System.out.println(t2.getName()+" "+t2.getId());
  }
}

运行结果:

main 1
Thread-0 9
Thread-1 10

yield():作用是让当前线程放弃cpu执行权,让给别的线程去占用CPU执行时间,但放弃时间长短不确定,可能刚放弃,又抢回CPU的执行权。

public class YieldTest {
    public static void main(String[] args) {
        ThreadTest t = new ThreadTest();
        t.start();
    }
}

ThreadTest中run方法为:

public void run(){
        long beginTime = System.currentTimeMillis();
        int count = 0;
        for(int i=0;i<500000;i++){
            //Thread.yield();
            count = count+i;
        }
        long endTime = System.currentTimeMillis();
        System.out.println("所需时间为:"+(endTime-beginTime)+"毫秒!");
}

结果为:2毫秒

去掉注释后为:93毫秒,CPU让给其他线程,导致结果变慢。

setPriority()和getPriority():

前者是设置线程优先级,后者是获得线程优先级。Java中优先级分为1~10级,超出这个范围会抛出Throw new IllegalArgumentException();

public class SetGetPriority {
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName()+"等级为:"
                            +Thread.currentThread().getPriority());
        //Thread.currentThread().setPriority(6);
        System.out.println(Thread.currentThread().getName()+"等级为:"
                +Thread.currentThread().getPriority());
        ThreadTest t = new ThreadTest();
        //t.setPriority(6);
        t.start();
    }
}
public void run(){
        System.out.println(this.currentThread().getName()+"的等级为:"+this.getPriority());
}

输出为:

main等级为:5
main等级为:5
Thread-0的等级为:5

去掉注释后结果后:

main等级为:5
main等级为:6
Thread-0的等级为:6

线程默认优先级为5,优先级越高不一定越先执行完,只能说概率更大一些。

原文地址:https://www.cnblogs.com/love-Stefanie/p/6732816.html