Java Concurrency Learning five--thread sleep and recovery

Source: Internet
Author: User

This article is a summary of the study of the article on the network, thank you for your selfless sharing.

1, the thread class of sleep method, you can make threads sleep. This method receives an integer as a parameter that indicates the number of milliseconds that the thread paused. After the sleep method is called, when the time is over, the JVM will schedule their CPU time and the thread will continue to execute as instructed.

Another possibility is to use a sleep method with a Timeunit enumeration element to let the current thread sleep using the sleep method of the thread class, but the parameter units it receives will turn into milliseconds.

2. When you call the Sleep () method, the thread leaves the CPU and stops running for a period of time. During this time, he is not consuming CPU time, using can perform other tasks,

But it does not release object locks.

When thread is asleep and interrupted, the method immediately throws a Interruptedexception exception and does not wait until the sleep time passes.

3. The Java Concurrency API has another way to get the thread object out of the CPU, which is the yield () method, which indicates to the JVM that the thread object allows the CPU to perform other tasks. The JVM does not guarantee compliance with the request, usually it is only used for debugging. It is just not possible for the user to specify how long to pause, and the yield () method only allows the same priority thread to have an opportunity to execute.

Package Chapter;import java.util.date;import java.util.concurrent.timeunit;/** * *  <p> * Description:  * </p> * @author Zhangjunshuai * @version 1.0 * Create date:2014-8-13 PM 8:47:30 * Project Name:java7thread * * <p re> * Modification History:   *             Date                                Author                   Version          Description  *----------------------- ------------------------------------------------------------------------------------   * Lastchange: $Date::             $      $Author: $          $Rev: $          * </pre> * */public class Fileclock implements runnable{@Overridepublic void Run () {for (int i = 0; i <; i++) {System.out.printf ("%s\n", New Date ()), try {TimeUnit.SECONDS.sleep (1);} catch (Interruptedexception e) {System.out.printf ("The Fileclock has been interrupted");}}}

Call class

Package Chapter;import Java.util.concurrent.timeunit;public class Main5 {/** * <p> * </p> * @author Zhangjunsh Uai * @date 2014-8-13 pm 6:34:34 * @param args */public static void main (string[] args) {Fileclock clock = new Fileclock (); Thread thread = new thread (clock); Thread.Start (); try {TimeUnit.SECONDS.sleep (5);} catch (Interruptedexception e) {// TODO auto-generated catch Blocke.printstacktrace ();} Thread.Interrupt ();}}

Reference:

Concurrent Network

Java yield (), sleep (), wait () differences-Memo Notes

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.