线程睡眠

线程睡眠:

方式: sleep(long millis)  线程睡眠 millis 毫秒            sleep(long millis, int nanos)  线程睡眠 millis 毫秒 + nanos 纳秒

说明:线程睡眠只能睡眠当前线程,因此一般写在Run()方法内。线程睡眠不会释放锁。

线程睡眠例子:

 1 public static void main(String[] args) throws InterruptedException{
 2         new Thread(){
 3             public void run(){
 4                 for(int i=0; i< 10; i++){
 5                     try {
 6                         Thread.sleep(1000);
 7                     } catch (InterruptedException e) {
 8                         // TODO Auto-generated catch block
 9                         e.printStackTrace();
10                     }
11                     System.out.println("aaa"+i);
12                 }
13             }
14         }.start();
15         
16         
17         new Thread(){
18             public void run(){
19                 for(int i=0; i< 10; i++){
20                     try {
21                         Thread.sleep(1000);
22                     } catch (InterruptedException e) {
23                         // TODO Auto-generated catch block
24                         e.printStackTrace();
25                     }
26                     System.out.println("bbb"+i);
27                 }
28             }
29         }.start();
30         
31         //        for(int i=10; i>0; i--){
32 //            Thread.sleep(1000);
33 //            System.out.println("倒计时"+i);
34 //        }
35     }
原文地址:https://www.cnblogs.com/feichangnice/p/10648569.html