Thread Manipulation API
CurrentThread
1. Thread. CurrentThread method
The static method of thread CurrentThread method can be used to get the thread running the current code fragment .
Thread current = Thread.CurrentThread ();
1 /**2 * Gets the thread of the current code block3 * @authorAdministrator4 *5 */6 7 classtestthreaddemo4{8 Public Static voidMain (string[] args) {9 /*Ten * The Os will start a process to run when the program is running . One * JVM, and a thread is created after the process starts, using this thread to run the Main method A */ - //This gets the thread that runs the main method -Thread T =Thread.CurrentThread (); theSYSTEM.OUT.PRINTLN ("The thread running the Main method is:" +t); - - //called in the Main method - testcurrent (); + //Creating Threads -Thread T1 =NewThread () { + @Override A Public voidrun () { at Super. Run (); -System.out.println ("Running T1 thread is:" +Thread.CurrentThread ()); - testcurrent (); - } - }; - T1.start (); in - to } + /** - * Outputs the thread that called the current method the */ * Public Static voidtestcurrent () { $SYSTEM.OUT.PRINTLN ("The thread that runs the Testcurrendt method is:" +Thread.CurrentThread ());Panax Notoginseng } -}
The thread that runs the Main method is: Thread[main,5,main]
The thread that runs the Testcurrendt method is: Thread[main,5,main]
Running the T1 thread is: Thread[thread-0,5,main]
The thread that runs the Testcurrendt method is: Thread[thread-0,5,main]
2. Get thread Information
Thread provides methods for getting thread information:
Long getId(): Returns the identifier of the thread globally unique cannot be duplicated
String getName(): Returns the name of the thread
int getpriority(): Returns the priority of the thread
Thread. State getState (): Gets the status of the thread
Boolean isAlive(): Test thread is active
Boolean Isdaemon(): Tests whether the thread is a daemon thread
Boolean isinterrupted(): Test thread has been interrupted
1 /**2 * methods to get thread information3 * @authorAdministrator4 *5 */6 classteatthreaddemo{7 Public Static voidMain (string[] args) {8 //gets the thread that called the Main method9Thread main =Thread.CurrentThread ();TenSystem.out.println ("ID:" +Main.getid ()); OneSystem.out.println ("Name:" +main.getname ()); ASystem.out.println ("Priority:" +main.getpriority ()); -System.out.println ("Status:" +main.getstate ()); -SYSTEM.OUT.PRINTLN ("Whether active:" +main.isalive ()); theSystem.out.println ("is daemon" +Main.isdaemon ()); -System.out.println ("Whether interrupted:" +main.isinterrupted ()); - } -}
3. Thread Priority
Thread switching is controlled by thread scheduling, and we cannot intervene through code, but we can maximize the chance of a thread acquiring a time slice by increasing the priority of the thread.
The priority of the thread is divided into 10 levels, with values of 1-10, of which 1 is the lowest and 10 is the highest. The thread provides 3 constants to represent the lowest, highest, and default precedence:
Thread.min_priority,
Thread.max_priority,
Thread.norm_priority
The priority is set in the following way:
void setpriority (int priority)
Thread Manipulation API