2019-08-19 线程休眠sleep方法

package com.foreknow.thread;
/**
 * 线程休眠sleep方法
 * static void sleep(long millis)在制定的毫秒数内让当前正在执行的线程休眠(暂停执行),休眠时间过后时间会自己恢复执行
 * @author Administrator
 *
 */
public class SleepDemo {
    public static void main(String[] args) {
//      Thread t = new Thread();
//      t.setName("线程1");//调用多次,覆盖
        new Thread("线程1"){//匿名对象,start调用一次
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println(getName()+"执行了");
                }
            }   
        }.start();
        
        new Thread("线程2"){
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println(getName()+"执行了");
                }
            }   
        }.start();
    }
}

你可能感兴趣的:(2019-08-19 线程休眠sleep方法)