Simple use of the thread's sleep () method

Source: Internet
Author: User

The thread's Sleep method signature bit:

public static void sleep (long Millis) throws Interruptexception is a static method that causes the currently executing thread to hibernate millis milliseconds

Package Com.demo;
Class Mythread implements runnable{public
	void Run (	
		int i = 0; i < 4; i++) {
			try {
				System.out.pri Ntln (Thread.CurrentThread (). GetName () + "Sleep 0.5 seconds!");
				Thread.Sleep (+);
			} catch (Interruptedexception e) {
				e.printstacktrace ();
			}
			System.out.println ("Currently running thread Name:" + Thread.CurrentThread (). GetName ());
}}} public class demo   {public
	static void Main (string[] args) {
		mythread mt1 = new Mythread ();
	    Thread th = new Thread (MT1, "thread A");
	    System.out.println ("\ n Thread is starting");
	    Th.start ();
	    try {
			thread.sleep (1000);
		} catch (Interruptedexception e) {
			
			e.printstacktrace ();
		}
        System.out.println ("\ n main thread is already dormant 2s" + thread.currentthread (). GetName ());	
	}



The results of the operation are as follows:

Thread is starting
Thread A sleeps for 0.5 seconds!
The currently running thread name: Thread A
Thread A sleeps for 0.5 seconds!
The currently running thread name: Thread A


Main thread has been dormant 1s main
Thread A sleeps for 0.5 seconds!
The currently running thread name: Thread A
Thread A sleeps for 0.5 seconds!
The currently running thread name: Thread A


From the results of the operation can be analyzed, when the thread a hibernate 1s, the main thread is also dormant 1s, the current running thread is main thread main, and then thread a continues to run.

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.