Java multithreading (medium)
In [major attack] Java getting started to proficient-multithreading (I), I explained in detail the thread and process, the two methods for creating the thread, and the thread life cycle. Here we will explain some thread control methods.Join of control thread
Join method: the thread object that calls the join method is forced to run. Other threads cannot run during the force running of the thread. Other threads can run only after the thread ends,Some people also regard this method as a federated thread.
The Code is as follows:
Public class joindemo {public static void main (string [] ARGs) throws exception {thread join = new thread (New Join (), "Join thread"); join. start (); int I = 0; while (I <500) {if (I = 100) {join. join ();} system. out. println ("Main -->" + I ++) ;}} class join implements runnable {public void run () {int I = 0; while (I <200) {system. out. println (thread. currentthread (). getname () + "--" + I ++ );}}}
Daemon
Background thread: runs in the background, and the task is to provide services for other threads. It is also called "daemon thread" or "Genie thread ". JVM garbage collection is a typical background thread.
Features: If all foreground threads die, background threads automatically die.
Set background thread: thread object setdaemon (true );
Setdaemon (true) must be before start () is called; otherwise, the illegalthreadstateexception occurs;
The foreground thread is created by default as the foreground thread;Determine whether it is a background thread: Use the isdaemon () method of the thread object;
The new thread is the background thread only when the creation thread is a background thread.
The Code is as follows:
Class daemon extends thread {public void run () {for (INT I = 0; I <10000; I ++) {system. out. println (getname () + "-" + I) ;}} public class daemondemo {public static void main (string [] ARGs) {daemon d = new daemon (); d. setdaemon (true); // set the D thread to the background thread D. start (); For (INT I = 0; I <5; I ++) {system. out. println (thread. currentthread (). getname () + "--" + I );}}}
Sleep of the control line
Thread sleep:
Pause the thread for a period of time and enter the blocking status. Note that this method throws an exception.
Sleep (long milllis) throws interruptedexception: millisecond
After sleep () is called, the thread will not receive execution opportunities within the specified period of time.
The Code is as follows:
Public class sleepdemo {public static void main (string [] ARGs) {for (INT I = 10; I> 0; I --) {system. out. println ("remaining" + I); try {thread. sleep (1000);} catch (interruptedexception e) {e. printstacktrace ();}}}}
Priority of the control process
Each thread has a priority, which is only related to the number of times the thread gets the execution opportunity. The higher the thread priority, it must be executed first. The first running of a thread depends on the CPU scheduling. By default, the main thread has a normal priority, and the thread it creates has a normal priority.
Setpriority (int x) and getpriority () of the thread object to set and obtain the priority.
Max_priority: The value is 10.
Min_priority: The value is 1.
Norm_priority: The value is 5 (the default priority of the main method)
The Code is as follows:
Class priority extends thread {public void run () {for (INT I = 0; I <100; I ++) {system. out. println (getname () + ", priority =" + getpriority () + "--" + I) ;}} public class prioritydemo {public static void main (string [] ARGs) {thread. currentthread (). setpriority (7); For (INT I = 0; I <100; I ++) {if (I = 10) {priority p1 = new priority (); p1.setpriority (thread. max_priority); p1.start () ;}if (I = 15) {priority P2 = new priority (); p2.setpriority (thread. min_priority); p2.start ();} system. out. println ("Main" + I );}}}
Yield
Thread courtesy:
Pause the currently executing thread object and execute other threads;
The static method of the thread, which can be paused by the current thread, but does not block the thread, but enters the ready state. Therefore, it is entirely possible that after a thread calls yield (), the thread scheduler schedules it again and re-executes it.
The Code is as follows:
Class yield implements runnable {public void run () {for (INT I = 0; I <100; I ++) {system. out. println (thread. currentthread (). getname () + "-->" + I); if (I % 2 = 0) {thread. yield (); // courtesy }}} public class yielddemo {public static void main (string [] ARGs) {yield y = new yield (); New thread (Y, ""). start (); New thread (Y, "C "). start ();}}
End thread of the control thread
We use a Boolean variable to control the end of a thread.
Boolean flag = true; // use a variable to control the thread public void run () {int I = 0; while (FLAG) {if (I = 25) {flag = false;} system. out. println ("=" + I); I ++ ;}}
API outdated method-Easy to deadlock and not recommended
Stop: Terminate the thread
Immediately stop the thread and release the lock held by the thread. This operation cannot ensure that the internal status of the object is correct;
Suspend: suspends a thread.
Enables the thread to enter the "blocking" State. In this state, the CPU will not be allocated to the thread time slice. This state can be used to pause the running of a thread and is unavailable before being called by the resume method.
If you want the target thread of suspend to lock an important system resource, no thread can use this resource until the target thread of suspend is resumed.
Resume: Recovery thread
Resume the thread suspended by the suspend Method