Java Multithreading 3: Static Methods in Thread

From: http://www.cnblogs.com/xrq730/p/4851344.html


Static method in Thread class

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:

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

Take a look at the running results:

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

This example shows that the constructor 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:

copy code
public class MyThread05 extends Thread
{
    public MyThread05()
    {
        System.out.println("MyThread5----->Begin");
        System.out.println("Thread.currentThread().getName()----->" + 
                Thread.currentThread().getName());
        System.out.println("this.getName()----->" + this.getName());
        System.out.println("MyThread5----->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");
    }
}
copy code
public static void main(String[] args)
{
    MyThread05 mt5 = new MyThread05();
    mt5.start();
}

看一下运行结果:

copy code
MyThread5----->Begin
Thread.currentThread().getName()----->main
this.getName()----->Thread-0
MyThread5----->end
run----->Begin
Thread.currentThread().getName()----->Thread-0
this.getName()----->Thread-0
run----->end
copy code

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

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

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

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

2、sleep(long millis)

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

copy code
public class MyThread07 extends Thread
{
    public void run()
    {
        try
        {
            System.out.println("run threadName = " + 
                    this.getName() + " begin");
            Thread.sleep(2000);
            System.out.println("run threadName = " + 
                    this.getName() + " end");
        } 
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}
copy code
copy code
public static void main(String[] args)
{
    MyThread07 mt = new MyThread07();
    System.out.println("begin = " + System.currentTimeMillis());
    mt.start();
    System.out.println("end = " + System.currentTimeMillis());
}
copy code

看一下运行结果:

begin = 1443694780609
end = 1443694780609
run threadName = Thread-0 begin
run threadName = Thread-0 end

当然,因为打印结果是静态的,所以只能看出异步执行的效果,看不出sleep(long millis)方法执行的效果。实际上第3句打出2秒后打出第4句,这和run()方法里面的sleep(2000)是对应的

3、yield()

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

copy code
public class MyThread08 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("用时:" + (endTime - beginTime) + "毫秒!");
    }
}
copy code
public static void main(String[] args)
{
    MyThread08 mt = new MyThread08();
    mt.start();
}

Take a look at the running results:

copy code
Time: 3264 milliseconds!
Time: 3299 milliseconds!
Time: 3232 milliseconds!
Time: 3256 milliseconds!
Time: 3283 milliseconds!
Time: 3504 milliseconds!
Time: 3378 milliseconds!
copy code

It can be seen that the time taken for each execution is different, which proves that the time for yield() method to give up the CPU is not certain.

4、interrupted()

Tests whether the current thread has been interrupted, and has the function of clearing the status flag to false after execution. In other words, if the method is called twice in a row, it must return false:

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

Of course, this also involves Java's interrupt mechanism, which will be explained in a later article.


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326900177&siteId=291194637