Java Thread Sleep example

Source: Internet
Author: User

Java Thread Sleep example

The java. lang. Thread sleep (long millis) method is used to pause the execution of the current Thread. The pause time is specified by the method parameter, in milliseconds. Note that the parameter cannot be negative. Otherwise, the program will throw IllegalArgumentException.

There is another sleep (long millis, int nanos) method, which has the same function as the preceding method, except that the pause time is millis millisecond plus nanos nanoseconds. The valid values of the nanoseconds are 0 to 0 ~ 999999.

The following program suspends the execution of the main Thread for 2 seconds by calling the Thread. sleep () method.

    main(String[] args)  start =2000"Sleep time in ms = "+(System.currentTimeMillis()-

Running the above program will show that the actual output sleep time is a little longer than 2000 milliseconds, which is mainly caused by the thread sleep working mechanism and the specific thread scheduling Implementation of the operating system.

Thread Sleep highlights
  • It is only used to pause the execution of the current thread.
  • The actual time for a thread to be awakened and started depends on the CPU time slice length of the operating system and the scheduling policy. For a relatively idle system, the actual sleep time is similar to the specified sleep time, but for a busy system, this time will look a little longer.
  • A thread does not release any acquired resources such as monitor and lock during sleep.
  • Any other thread can interrupt the current sleep thread and throw InterruptedException.
Thread sleep Working Mechanism

Thread. sleep () notifies the Thread scheduler to set the current Thread to the wait status in the specified time period. When the wait time ends, the thread status changes to Runnable and waits for the CPU to be rescheduled again. Therefore, the actual thread sleep time depends on the thread scheduler, which is completed by the operating system.

Link: http://www.journaldev.com/1020/java-thread-sleep-example

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.