Java 中sleep指定毫秒 – yasi

http://www.tutorialspoint.com/java/lang/thread_sleep_millis.htm

Description

Thejava.lang.Thread.sleep(long millis)method causes the currently executing thread to sleep for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.

Declaration

Following is the declaration forjava.lang.Thread.sleep()method

publicstaticvoid sleep(long millis)throwsInterruptedException

Parameters

millis– This is the length of time to sleep in milliseconds.

Return Value

This method does not return any value.

Exception

InterruptedException– if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

Example

The following example shows the usage of java.lang.Thread.sleep() method.

package com.tutorialspoint;import java.lang.Thread;publicclassThreadDemoimplementsRunnable{Thread t;publicvoid run(){for(int i =10; i <13; i++){System.out.println(Thread.currentThread().getName()+"  "+ i);try{// thread to sleep for 1000 millisecondsThread.sleep(1000);}catch(Exception e){System.out.println(e);}}}publicstaticvoid main(String[] args)throwsException{Thread t =newThread(newThreadDemo());// this will call run() function      t.start();Thread t2 =newThread(newThreadDemo());// this will call run() function      t2.start();}}

Let us compile and run the above program, this will produce the following result:

Thread-010Thread-110Thread-011Thread-111Thread-012Thread-112

困难是人的教科书。

Java 中sleep指定毫秒 – yasi

相关文章:

你感兴趣的文章:

标签云: