Java Multi-Threading common method (naming and acquisition) __java

Source: Internet
Author: User

The naming and acquisition of threads
The execution of all threaded routines, each of which is a different running result, must depend on the thread's name if it is to differentiate each thread. For the name of the thread, it is generally defined before the start of the process, and it is not recommended to change the name of the thread that has started, or to set the duplicate names for different threads
If you want to do a thread name operation, you can use the following methods of the thread class:
* Construction Method: Public Thread (Runnable target,string name)
* Set name public final void SetName (String name)
* Get name ' public final String getName ()

These methods belong to the thread class, switch to runnable, and if you want to get a name, you can get the name of the thread that is currently executing this method, so there is an object in the thread class that gets the current thread
*public static Thread CurrentThread ()
If you do not set a name for the thread class object when it is instantiated, the number is automatically renamed without repeating it

public class Mythread implements Runnable    {
    @Override public
    void Run () {
        System.out.println ( Thread.CurrentThread (). GetName ());

    }

public class Testdemo {public

    static void Main (string[] args)  {
        mythread mt = new Mythread ();
        Name: New Thread (MT, thread A). Start ();
        New Thread (MT). Start ();
        New Thread (MT). Start ();
        New Thread (MT). Start ();
    }


Output Result: Thread-0
Thread-2
Thread-1

The main method is a thread (main thread), and all threads created on the main method can actually represent it as a child thread

public class Mythread implements Runnable    {
    @Override public
    void Run () {
        System.out.println ( Thread.CurrentThread (). GetName ());

    }

public class Testdemo {public

    static void Main (string[] args)  {
        mythread mt = new Mythread ();
        New Thread (MT, thread A). Start ();
        Mt.run ()//Direct call to Run,main
    }

}

Output Result: Main
Thread A

Threads actually always exist (the main method is the primary thread), and each JVM process starts at least 2 threads:
Main thread: The primary execution of the program, and the Boot child thread
GC Thread: Responsible for garbage collection

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.