Four. Java multi-threading blocking state and line programming
Several specific types of Java blocking have been mentioned above. The main methods that cause Java thread blocking are shown below.
1.join ()
join--a thread to wait for another thread to finish before continuing. If a thread is called the Join () method in the thread execution body, the a thread is blocked, and until the B thread finishes executing, a will be able to continue execution.
1 public class ThreadTest {2 3 public static void Main (string[] args) {4 5 myrunnable myrunnable = new M Yrunnable (); 6 Thread thread = new Thread (myrunnable), 7 8 for (int i = 0; i < i++) {9 System.out.println (thre Ad.currentthread (). GetName () + "" + i), if (i = =) {One Thread.Start (), and try { thread.join (); c12/>//the main thread needs to wait for the thread thread to execute before it can continue to execute the \ catch (interruptedexception e) { e.printstacktrace (); }18 }19 }20}21 class Myrunnable implements Runnable {@Override25 public void Run () {$ for (int i = 0; i < i++) { System.out.println (Thread.CurrentThread (). GetName () + "" + i); }29 }30}
2.sleep ()
sleep--lets the current executing thread pause for a specified time and into a blocking state. During its sleep period, the thread is not in a ready state and therefore does not get an opportunity to execute. Even if there are no other executable threads in the system at this time, the threads in sleep () will not execute. Therefore, the sleep () method is often used to pause thread execution.
As mentioned earlier, when the start () method of the newly created thread is called, the thread enters the ready state, the CPU time slice may be fetched at a later time, and if you want this new thread to execute immediately, call the original thread's sleep (1) directly.
1 public class ThreadTest {2 3 public static void Main (string[] args) {4 5 myrunnable myrunnable = new M Yrunnable (); 6 Thread thread = new Thread (myrunnable), 7 8 for (int i = 0; i < i++) {9 System.out.println (thre Ad.currentthread (). GetName () + "" + i), if (i = =) {One Thread.Start (), and try { thread.sleep (1); //Make thread must be able to execute immediately catch (Interruptedexception e) { e.printstacktrace (), }17 }18 }19 }20}21 Myrunnable implements Runnable {@Override25 public void Run () { (int i = 0; i <; i++) {2 7 System.out.println (Thread.CurrentThread (). GetName () + "" + i); }29 }30}
Note: Sleep a millisecond is enough because the CPU is not idle and will switch to the new thread.
3. Background threads (Daemon thread)
Concept/Purpose: A background thread is primarily a service, or "daemon thread", for other threads, which can be referred to as foreground threads. such as garbage collection threads in the JVM.
Life cycle: The life cycle of a background thread is associated with the foreground thread life cycle. This is mainly reflected in: when all the foreground threads are in the dead state, the background thread will die automatically (in fact, this is very well understood, because the background thread exists to serve the foreground thread, since all the foreground threads are dead, then it still has what use ...). Great!!).
Set Background thread: invokes the Setdaemon (true) method of the thread object to set the specified thread as a background thread.
1 public class ThreadTest {2 3 public static void Main (string[] args) {4 Thread myThread = new MyThread (); 5 for (int i = 0; i < i++) {6 System.out.println ("main thread i =" + i); 7 if (i = =) {8 Mythread.setdaemon (TRUE); 9 Mythread.start (); }11 }12}13}15- class M Ythread extends Thread {n] public void Run () { 0 (int i = +; i <; i++) { System.out.println ("i =" + i); try { thread.sleep (1);] catch (interruptedexception e) { //TODO Auto-generate D Catch block25 e.printstacktrace (); }27 }28 }29}
Determines whether a thread is a background thread: invokes the Isdeamon () method of the Thread object.
Note: The main thread is the foreground thread by default, the child thread created in foreground thread creation is the foreground thread by default, and the thread created in the background thread defaults to the background thread. Calling the Setdeamon (true) method to set the foreground thread to a background thread is required before the start () method call. After the thread died the day before, the JVM notifies the background thread of death, but it takes some time from receiving the instruction to responding.
4. Change the priority of the thread/setpriority ():
Each thread has a certain priority at execution time, and higher priority threads have more execution opportunities. The default priority for each thread is the same as the thread that created it. The main thread has a normal priority by default.
Set Thread Priority: setpriority (int prioritylevel). The parameter Prioritylevel range is between 1-10 and is commonly used with the following three static constant values:
Max_priority:10
Min_priority:1
Norm_priority:5
Get Thread Priority: GetPriority ().
Note: A thread object with a higher thread priority only means that this thread has more execution opportunities than priority execution.
1 public class ThreadTest {2 3 public static void Main (string[] args) {4 Thread myThread = new MyThread (); 5 for (int i = 0; i < i++) {6 System.out.println ("main thread i =" + i); 7 if (i = =) {8 mythread.setpriority (thread.max_priority); 9 Mythread.start (); }11 }12 }13}15 class MyThread extends Thread {n] public void Run () { 0; i <; i++) { System.out.println ("i =" + i); }22 }23}
5. Thread Concession: Yield ()
The basic role of yield () is already mentioned in the previous blog post, and the yield () method is also related to thread precedence, when a thread calls the Yiled () method from the running state to the ready state. The CPU from the Ready state thread queue only chooses the thread with the same priority or higher priority of the thread to execute.
1 public class ThreadTest {2 3 public static void main (string[] args) {4 Thread myThread1 = new Mythr Ead1 (); 5 Thread myThread2 = new MyThread2 (); 6 mythread1.setpriority (thread.max_priority); 7 mythread2.setpriority (thread.min_priority); 8 for (int i = 0; i < i++) {9 System.out.println ("main thread i =" + i); = =) {one mythread1.start (); Mythread2.start (); Thread.yield (); 14 }15}16}17}19 class MyThread1 extends Thread {$ public void run () { NT i = 0; I < 100; i++) {System.out.println ("MyThread 1--i =" + i);}26}27}28 class MyThread2 extends T Hread {The public void run () {0 (int i = +; i <; i++) {System.out.println ("Mythre Ad 2--i = "+ i);}35}36}
Java Summary series: Java multithreading (two)