Java Multithreading 6: Static Methods in Thread

Static method in Thread classfast to the end

A static method in the Thread class indicates that the thread of operation is "the thread that is executing the block of code where the static method is located ". Why should there be static methods in the Thread class, so that the thread that is currently running on the CPU can be operated. Let's take a look at the static methods in the Thread class:

1、currentThread()

The currentThread() method returns a reference to the currently executing thread object . Look at an important example and conclude:

public class MyThread extends Thread
{
    static
    {
        System.out.println( "Printing of static block: " + Thread.currentThread().getName());    
    }
    
    public MyThread()
    {
        System.out.println( "Printing of constructor: " + Thread.currentThread().getName());    
    }
    
    public void run()
    {
        System.out.println( "print of run() method: " + Thread.currentThread().getName());
    }
    
    // Test method 
    public  static  void main(String[] args)
    {
        MyThread mt = new MyThread();
        mt.start();
    }
}

operation result

Printing of static block: main
Constructor print: main
The print of the run() method: Thread -0

This example shows that the construction method and static block of the thread class are called by the main thread, while the run() method of the thread class is called by the application thread itself . On the basis of this example, go deeper:

public class MyThread extends Thread
{
    public MyThread()
    {
        System.out.println("MyThread----->Begin");
        System.out.println("Thread.currentThread().getName()----->" + Thread.currentThread().getName());
        System.out.println("this.getName()----->" + this.getName());
        System.out.println("MyThread----->end");
    }
    
    public void run()
    {
        System.out.println("run----->Begin");
        System.out.println("Thread.currentThread().getName()----->" + Thread.currentThread().getName());
        System.out.println("this.getName()----->" + this.getName());
        System.out.println("run----->end");
    }
    
    // Test method 
    public  static  void main(String[] args)
    {
        MyThread mt = new MyThread();
        mt.start();
    }
}

operation result

MyThread----->Begin
Thread.currentThread().getName()----->main
this.getName()----->Thread-0
MyThread----->end run----->Begin Thread.currentThread().getName()----->Thread-0 this.getName()----->Thread-0 run----->end

上篇文章的开头就说过,要理解一个重要的概念,就是 "this.XXX()" 和 "Thread.currentThread().XXX()" 的区别,这个就是最好的例子。必须要清楚的一点就是:当前执行的 Thread 未必就是 Thread 本身。从这个例子就能看出来:

(1)执行 MyThread 构造方法是 main,当前线程却是 Thread-0

(2)执行 run() 方法的 Thread-0,当前线程也是 Thread-0,说明 run() 方法就是被线程实例去执行的

所以,再强调一下,未必在 MyThread 里调用 Thread.currentThread() 返回回来的线程对象的引用就是 MyThread

2、sleep(long millis)

sleep(long millis) 方法的作用是在指定的毫秒内让当前"正在执行的线程"休眠(暂停执行)。这个"正在执行的线程"是关键,指的是 Thread.currentThread() 返回的线程。根据J DK API 的说法,"该线程不丢失任何监视器的所属权",简单说就是 sleep 代码上下文如果被加锁了,锁依然在,但是 CPU 资源会让出给其他线程。例子就不写了,很简单。

3、yield()

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

public class MyThread extends Thread
{
    public void run()
    {
        long beginTime = System.currentTimeMillis();
        int count = 0;
        for (int i = 0; i < 50000000; i++)
        {
            Thread.yield();
            count = count + i + 1;
        }
        long endTime = System.currentTimeMillis();
        System.out.println("count:"+count);
        System.out.println("用时:" + (endTime - beginTime) + "毫秒!");
    }
    // 测试方法
    public static void main(String[] args)
    {
        MyThread mt = new MyThread();
        System.out.println("begin = " + System.currentTimeMillis());
        mt.start();
        
        System.out.println("end = " + System.currentTimeMillis());
    }
}

运行结果

begin = 1524485060250
end = 1524485060251
count:1333106752
用时:6249毫秒!
----------------------------
begin = 1524485136599
end = 1524485136600
count:1333106752
用时:6447毫秒!

----------------------------
begin = 1524485162837
end = 1524485162837
count:1333106752
用时:6234毫秒!

看到,每次执行的用时都不一样,证明了 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!");
}

当然,这也涉及 Java 的中断机制,留在后面的一篇文章专门讲解。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324814113&siteId=291194637