1. Multi-threaded blocking status
join (): A thread calls the join () method and must wait for the other thread to finish before executing
Packagejsontest; Public classRunabledemoImplementsRunnable {@Override Public voidrun () {inti = 0 ; for(; i<10;i++) {System.out.println ("-----------" +thread.currentthread (). GetName () + "----" +i); } } Public Static voidMain (string[] args) {Runabledemo d=NewRunabledemo (); Thread T=NewThread (d); intK = 0 ; for(; k<10;k++) {System.out.println ("---------" +thread.currentthread (). GetName () + "----" +k); if(k==5) {T.start (); Try{t.join (); } Catch(interruptedexception e) {e.printstacktrace (); } }}} results:---------Main----0---------main----1---------main----2---------main----3---------main----4---------main----5-----------Th read-0----0-----------Thread-0----1-----------Thread-0----2-----------Thread-0----3-----------Thread-0----4-----------Thr ead-0----5-----------Thread-0----6-----------Thread-0----7-----------Thread-0----8-----------Thread-0----9---------main-- --6---------Main----7---------main----8---------main----9
2.sleep (): Let the current thread pause after the specified time, sleep (1): Execute now
Packagejsontest; Public classRunabledemoImplementsRunnable {@Override Public voidrun () {inti = 0 ; for(; i<10;i++) {System.out.println ("-----------" +thread.currentthread (). GetName () + "----" +i); } } Public Static voidMain (string[] args) {Runabledemo d=NewRunabledemo (); Thread T=NewThread (d); intK = 0 ; for(; k<10;k++) {System.out.println ("---------" +thread.currentthread (). GetName () + "----" +k); if(k==5) {T.start (); Try{Thread.Sleep (5000);//after the thread threads execute, the main thread is executed after 5 seconds}Catch(interruptedexception e) {e.printstacktrace (); } } } }}
3. Background threads (Daemon thread)
Target: A background thread is primarily a service, or "daemon", 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.
Packagejsontest; Public classRunabledemoImplementsRunnable {@Override Public voidrun () {inti = 0 ; for(; i<1000;i++) {System.out.println ("-----------" +thread.currentthread (). GetName () + "----" +i); } } Public Static voidMain (string[] args) {Runabledemo d=NewRunabledemo (); Thread T=NewThread (d); intK = 0 ; for(; k<10;k++) {System.out.println ("---------" +thread.currentthread (). GetName () + "----" +k); if(k==5) {T.Setdaemon ( true);//set the thread thread to be a background thread, and after the current main thread has finished executing, the background thread will end up.T.start (); } } }}
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 indicates that this thread has more execution opportunities than a priority execution
Packagejsontest; Public classRunabledemoImplementsRunnable {@Override Public voidrun () {inti = 0 ; for(; i<100;i++) {System.out.println ("-----------" +thread.currentthread (). GetName () + "----" +i); } } Public Static voidMain (string[] args) {Runabledemo d=NewRunabledemo (); Thread T=NewThread (d); intK = 0 ; for(; k<100;k++) {System.out.println ("---------" +thread.currentthread (). GetName () + "----" +k); if(k==5) {t.setpriority (thread.max_priority); T.start (); } } }}
5. Thread Concession: yield ()
The yield () method thread priority relates to threads with the same priority or higher priority in the CPU-ready state thread queue when a thread calls the Yiled () method from the running state to the ready state, with more execution opportunities
Packagejsontest; Public classRunabledemoImplementsRunnable {@Override Public voidrun () {inti = 0 ; for(; i<100;i++) {System.out.println ("-----------Thread 1----" +i); } } Public Static voidMain (string[] args) {Runabledemo d=NewRunabledemo (); RunableDemo2 R=NewRunableDemo2 (); Thread T=NewThread (d); Thread T2=NewThread (R); T.setpriority (thread.max_priority); T2.setpriority (thread.min_priority); intK = 0 ; for(; k<100;k++) {System.out.println ("---------" +thread.currentthread (). GetName () + "----" +k); if(k==20) {T.start (); T2.start (); Thread.yield (); } } }}classRunableDemo2ImplementsRunnable {@Override Public voidrun () {inti = 0 ; for(; i<100;i++) {System.out.println ("--------Thread 2----" +i); } } }
Reference: http://www.cnblogs.com/lwbqqyumidi/p/3817517.html
Java Multithreading (two)