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