Java thread Summary (1)

Source: Internet
Author: User

1. The join () method is waiting for the thread to be joined to complete execution. The join (long millis) method can be used to join a new car for a maximum of minllis milliseconds. If the join thread has not finished execution within millis milliseconds, it will not wait. The join (long millis, int nanos) method waits for the join time to be up to millis millisecond plus nanos nanoseconds. Case: package com. xiaomo. thread; public class JoinThread extends Thread {// provides a parameter constructor for setting the thread name public JoinThread (String name) {super (name );} // override the run method and define the thread execution body public void run () {for (int I = 0; I <100; I ++) {System. out. println (getName () + "" + I) ;}} public static void main (String [] args) throws Exception {// promoter thread new JoinThread ("new thread "). start (); for (int I = 0; I <100; I ++) {if (I = 20) {JoinThread Jt = new JoinThread ("joined thread"); jt. start (); // The main thread calls the join method of the jt thread. The main thread must wait until the execution of the jt thread ends before executing the jt thread. join ();} System. out. println (Thread. currentThread (). getName () + "" + I) ;}}ii. setPriority () and Method: set the value and value, and return the priority of the thread. The setPriority can be an integer, the value range is one to ten. It can also be three static constants: MAX_PRIORITY, MIN_PRIORITY, and NORM_PRIORITY. The getPriority () method returns the priority of the thread. Each thread has a certain priority during execution. A thread with a higher priority gets more execution opportunities, while a thread with a lower priority gets a smaller execution opportunity. Case: package com. xiaomo. thread; public class PriorityThread extends Thread {// defines a constructor with parameters. It is used to specify namepublic PriorityThread (String name) {super (name) when creating a thread );} public void run () {for (int I = 0; I <50; I ++) {System. out. println (getName () + ", the priority is:" + getPriority () + ", the value of the loop variable is:" + I );}} public static void main (String [] args) {// change the priority of the main Thread. currentThread (). setPriority (6); for (int I = 0; I <30; I ++) {if (I = 10) {PriorityThread low = new PriorityThread ("low-level"); low. start (); System. out. println ("priority at the beginning of creation:" + low. getPriority (); // set this thread to the lowest priority low. setPriority (Thread. MIN_PRIORITY);} if (I = 20) {PriorityThread high = new PriorityThread ("advanced"); high. start (); System. out. println ("priority at the beginning of creation:" + high. getPriority (); // sets this thread to the highest priority. setPriority (Thread. MAX_PRIORITY) ;}}}3. A DaemonThread runs in the background and its tasks are A thread provides services. This kind of thread is called a backend thread, also known as a daemon thread, or a genie thread. The JVM garbage collection thread is a typical background thread. Background threads have a feature: If all foreground threads die, background threads automatically die. Call the setDaemon (blooean on) method of the Thread object (pass the parameter to true) to set the specified Thread to a background Thread, and isDaemon () to determine whether the Thread is a background Thread, if the thread is a daemon thread, true is returned; otherwise, false is returned. The sleep (long millis) method suspends the currently running thread and becomes congested. This method is affected by the accuracy and accuracy of the system timer and thread scheduler. Note: When the current thread calls the sleep method and enters the blocking state, the thread will not get the execution opportunity during the sleep period, even if there are no other running threads in the system, the thread in the sleep period does not run either. Therefore, sleep is often used to pause program execution. Case: package com. xiaomo. thread; public class DaemonThread extends Thread {// rewrite the run method and define the thread execution body public void run () {for (int I = 0; I <10000; I ++) {System. out. println (getName () + "" + I) ;}} public static void main (String [] args) throws InterruptedException {DaemonThread t = new DaemonThread (); // set this thread to a background thread (which must be set before startup) t. setDaemon (true); // start the background thread t. start (); for (int I = 0; I <100; I ++) {Sy Stem. out. println (Thread. currentThread (). getName () + "" + I);} // ------ the program is executed here, the foreground thread (main thread) end ------ // The background thread should also end.} 4. The yield () method (thread concession) yield method is similar to the sleep method to suspend the currently executing thread, but it does not block the thread. It only transfers the thread to the ready state. The yield method only suspends the current thread and schedules the system thread scheduler again, when a thread calls the yield method to suspend a thread, only the threads with the same priority as the current thread or with a higher priority than the current thread will get the execution opportunity. Example: package com. xiaomo. thread; public class YieldThread extends Thread {public YieldThread (String name) {super (n Ame);} // defines the run method as the thread execution body public void run () {for (int I = 0; I <100; I ++) {System. out. println (getName () + "" + I); if (I = 20) {Thread. yield () ;}} public static void main (String [] args) {// start two concurrent threads YieldThread yt1 = new YieldThread ("advanced "); // set the yt1 Thread to the highest priority yt1.setPriority (Thread. MAX_PRIORITY); yt1.start (); YieldThread yt2 = new YieldThread ("low-level"); // set the yt2 Thread to the lowest priority yt2.setPriority (Thread. MIN_PRIORITY); yt 2. start () ;}}5. Differences between the sleep method and the yield method 1. After the sleep method suspends the current thread, it will give other threads a chance to execute it, regardless of the priority of other threads, however, the yield method only gives the thread execution opportunity with the same or higher priority. 2. The sleep method transfers the thread to the blocking state until it passes through the blocking time. The yield method does not transfer the thread to the blocking state, it only forces the current thread to enter the ready state, so it is entirely possible that a thread will immediately obtain the processor resource to be executed after being paused using the yield method. 3. The sleep method declaration throws an InterruptedException exception. When calling the sleep method, you can either catch the exception or display the Declaration to throw the exception. The yield method does not declare that any exception is thrown. 4. The sleep method has better portability than the yield method. Generally, the yield method is not used to control the execution of concurrent threads. Case: package com. xiaomo. thread; import java. util. date; public class SleepThread {public static void main (String [] args) throws InterruptedException {for (int I = 0; I <10; I ++) {System. out. println ("current time:" + new Date () + "--->" + I); // call the sleep method to suspend the current thread for 1sThread. sleep (1000 );}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.