四、Thread类中的静态方法

1、静态方法等的调用者

Thread类中的静态方法表示操作的线程是"正在执行静态方法所在的代码块的线程"。
为什么Thread类中要有静态方法?
这样就能对CPU当前正在运行的线程进行操作。下面来看一下Thread类中的静态方法:

  • currentThread()方法返回的是对当前正在执行线程实例对象的引用
    代码示例:
public class Curr_JobRun extends Thread{
    static{
        System.out.println("static  currentThread:    "+Thread.currentThread().getName());
    }
    public Curr_JobRun() {
        System.out.println("construnctor  currentThreadName:    "+Thread.currentThread().getName());
        System.out.println("construnctor  this.ThreadName:    "+this.getName());
    }
    @Override
    public void run(){
        System.out.println("run  currentThreadName:    "+Thread.currentThread().getName());
        System.out.println("run  this.ThreadName:    "+this.getName());
    }
}


public static void main(String[] args) throws InterruptedException {
      System.out.println("Main Thread Name:    "+Thread.currentThread().getName());
      Curr_JobRun cjr=new Curr_JobRun();
      cjr.start();
    }

结果:
Main Thread Name: main
static currentThread: main
construnctor currentThreadName: main
construnctor this.ThreadName: Thread-0
run currentThreadName: Thread-0
run this.ThreadName: Thread-0

总结:可以从结果上看到 静态代码块取得是 调用者的线程名称,而通过“this” 却能拿到实例对象的线程
线程类的构造方法、静态块是被main线程调用的,而线程类的run()方法才是应用线程自己调用的
注意:
(1)执行Curr_JobRun构造方法是main,当前线程却是Thread-0
(2)执行run()方法的Thread-0,当前线程也是Thread-0,说明run()方法就是被线程实例去执行的
所以,再强调一下,未必在Curr_JobRun里调用Thread.currentThread()返回回来的线程对象的引用就是Curr_JobRun

2、sleep(long millis)

sleep(long millis)方法的作用是在指定的毫秒内让当前"正在执行的线程"休眠(暂停执行)。这个"正在执行的线程"是关键,
指的是Thread.currentThread()返回的线程。根据JDK API的说法,“该线程不丢失任何监视器的所属权”,简单说就是sleep代码上下文如果被加锁了,锁依然在,但是CPU资源会让出给其他线程。
具体可以参考:
https://www.cnblogs.com/ILove/archive/2008/04/07/1140419.html

3、yield()

暂停当前执行的线程对象,并执行其他线程。这个暂停是会放弃CPU资源的,并且放弃CPU的时间不确定,有可能刚放弃,就获得CPU资源了,也有可能放弃好一会儿,才会被CPU执行。
代码示例:

public  void run() {
        Long t1=System.currentTimeMillis();
        for (int i=0;i<10000000;i++){
            Thread.yield();
        }
        Long t2=System.currentTimeMillis();
        System.out.println(this.getName()+" \t执行的耗时:"+(t2-t1));
    }
	
 public static void main(String[] args) throws InterruptedException {
      for(int i=0;i<5;i++){
          Yield_JobRun yield_jobRun=new Yield_JobRun();
          yield_jobRun.start();
      }
    }

结果:
Thread-0 执行的耗时:6759
Thread-4 执行的耗时:6822
Thread-1 执行的耗时:6822
Thread-2 执行的耗时:6861
Thread-3 执行的耗时:6871

总结:看到,每次执行的用时都不一样,证明了yield()方法放弃CPU的时间并不确定。

4、interrupted()

测试当前线程是否已经中断,执行后具有将状态标识清除为false的功能。换句话说,如果连续两次调用该方法,那么返回的必定是false:

代码示例:

public static void main(String[] args)
{
    Thread.currentThread().interrupt();
    System.out.println("是否停止1?" + Thread.interrupted());
    System.out.println("是否停止2?" + Thread.interrupted());
    System.out.println("end!");
}

结果:
是否停止1?false
是否停止2?false
end!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值