java Thread线程方法详解

Thread源码知识:

3、currentThread()方法
返回当前正在被那个线程调用

 static class MyThread extends Thread {

        public MyThread() {
            System.out.println("MyThread constructor begin");
            System.out.println("Thread.currentThread().getName()=" + Thread.currentThread().getName());
            System.out.println("this.getName()=" + this.getName());
            System.out.println("MyThread constructor end");
            System.out.println();
            System.out.println();
        }

        @Override
        public void run() {
            super.run();
            System.out.println("MyThread run begin");
            System.out.println("Thread.currentThread().getName()=" + Thread.currentThread().getName());
            System.out.println("this.getName()=" + this.getName());
            System.out.println("MyThread run end");
        }
    }

测试方法1

public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.setName("A");
        myThread.start();
 }

结果:
MyThread constructor begin
Thread.currentThread().getName()=main //R1
this.getName()=Thread-0 //R2
MyThread constructor end

MyThread run begin
Thread.currentThread().getName()=A //R3
this.getName()=A //R4
MyThread run end

结果分析
R1:此线程由主线程new创建,构造函数也就由主线程来执行
R2:每个线程创建的时候,都会默认的初始化一个线程名字,这部分在setName之前执行
R3:A线程正在执行
R4:setName覆盖了myThread的默认线程名字(Thread-0)

测试方法2:

   public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.setName("B");
        Thread proxyThread = new Thread(myThread);
        proxyThread.start();
    }

结果:
MyThread constructor begin
Thread.currentThread().getName()=main //R1
this.getName()=Thread-0 //R2
MyThread constructor end

MyThread run begin
Thread.currentThread().getName()=Thread-1 //R3
this.getName()=B //R4
MyThread run end

R1:此线程由主线程new创建,构造函数也就由主线程来执行
R2:每个线程创建的时候,都会默认的初始化一个线程名字,这部分在setName之前执行
R3:proxyThread 默认的线程名称,执行了myThread 的run方法
R4:

4、isAlive()方法
判断当前线程是否处于活动状态,活动状态就是线程启动尚未终止

9、yield()方法
放弃当前CPU资源,让给其他任务去占用CPU时间,但放弃的时间不确定,有可能刚刚放弃就马上获得CPU的时间片

public class Test {
    static class MyThread extends Thread {
        @Override
        public void run() {
            long beginTime = System.currentTimeMillis();
            for (int i = 0; i < 100000; i++) {
                yield();
                new String();
                new String();
                new String();
                new String();
            }
            long endTime = System.currentTimeMillis();
            System.out.println("use time:" + (endTime - beginTime));
        }
    }
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();
    }
}

没注释掉yield方法结果为:use time:48
注释掉yield方法结果为:use time:15

10、线程的优先级
A、线程的优先级范围为1~10,最大的优先级为10,最小的优先级为1,线程默认的优先是5,线程的三个常量
public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;
B、优先级的继承性,A线程创建了B线程,则B线程的优先级与A相同
C、线程的规则性:高有优先级的线程总是大部分先执行完,并不代表高优先级的线程先全部执行完,因为CPU尽量会将执行资源让给优先级高的线程
D、线程的随机性:线程抢占机制,导致即使高优先级的线程也不一定先执行完成

11、守护线程
当进程中不存在非守护线程,则守护线程将自动销毁。典型的垃圾回收线程就是守护线程。

public class Test {
    static class MyThread extends Thread {
        private long counter = 1;
        @Override
        public void run() {
            try {
                while (true) {
                    if (isInterrupted()) {
                        throw new InterruptedException("被中断了!!!");
                    }
                    Thread.sleep(1000);
                    System.out.println("run counter=" + (counter++));
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
        MyThread myThread = new MyThread();
        myThread.setDaemon(true);
        myThread.start();
        Thread.sleep(5000);
        System.out.println("main end");
    }
}

运行结果
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值