The static method in Java multithreading 3:thread

Source: Internet
Author: User
Tags thread class

Static methods in the thread class

The static method in the thread class represents the thread of the operation that is executing the thread of the code block where the static method resides . Why there is a static method in the thread class so that it can operate on the thread that the CPU is currently running . Here's 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 then come to the conclusion that:

Look at the results of the operation:

Printing of static blocks: the printing of the main construction method: Mainrun () method printing: Thread-0

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

As I said at the beginning of the previous article, to understand an important concept is thedistinction between "this.xxx ()" and "Thread.CurrentThread (). XXX ()", this is the best example. It must be clear that the thread that is currently executing is not necessarily the thread itself . From this example you can see:

(1) Execution MyThread05 constructor method is main, the current thread is Thread-0

(2) Executing the Thread-0 of the Run () method, the current thread is also Thread-0, indicating that the run () method is executed by the thread instance.

So, again, it is not necessary to call Thread.CurrentThread () in MyThread05 to return a reference to the thread object that is MYTHREAD05

2, Sleep (long Millis)

The function of the sleep (long Millis) method is to let the currently executing thread hibernate (paused) within the specified millisecond. This "executing thread" is the key, referring to the thread returned by Thread.CurrentThread () . According to the JDK API, "the thread does not lose ownership of any monitor", and, to be blunt, it does not yield CPU resources. The CPU is also executing the code in the current thread's run () method, nothing more than "sleep". Take a look at the example:

Of course, because the print results are static, you can only see the effect of the asynchronous execution, not the effect of the sleep (long Millis) method execution. In fact, the 3rd sentence played 2 seconds after the 4th sentence, this and the run () method inside the Sleep (2000) is the corresponding

3. Yield ()

Pauses the currently executing thread object and executes other threads. This pause is the CPU resources will be discarded, and the time to abandon the CPU is uncertain, it is possible to just give up, to obtain CPU resources, it is possible to give up for a few moments before the CPU is executed. Take a look at the example:

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 ("spents:" + (Endtime-begintime) + "milliseconds! ");    }}
public static void Main (string[] args) {    MyThread08 mt = new MyThread08 ();    Mt.start ();}

Look at the results of the operation:

Spents: 3264 milliseconds! Spents: 3299 milliseconds! Spents: 3232 milliseconds! Spents: 3256 milliseconds! Spents: 3283 milliseconds! Spents: 3504 milliseconds! Spents: 3378 milliseconds!

See, each execution is different, proving that the yield () method abandons the CPU time is not deterministic.

4, Interrupted ()

Tests if the front thread has been interrupted and has been executed with the ability to clear the status ID to False. In other words, if the method is called twice in a row, then the return must be false:

public static void Main (string[] args) {    thread.currentthread (). interrupt ();    System.out.println ("Do you want to stop 1? "+ thread.interrupted ());    System.out.println ("Do you want to stop 2? "+ thread.interrupted ());    System.out.println ("end!");}

The static method in Java multithreading 3:thread

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.