5、sleep()

版权声明:版权归 爱装逼的文艺小青年所有 https://blog.csdn.net/toyota_rav4/article/details/84791608

sleep()的作用:让当前正在执行的线程在指定的毫秒数内休眠(暂停执行),

这个正在执行的线程 是指this.currentThread()返回的线程。

package com.com.demo6;

import java.text.SimpleDateFormat;

public class FormatUtil {
    public static String formatTime(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(System.currentTimeMillis());
    }
}
package com.com.demo6;

public class MyThread extends Thread {
    @Override
    public void run() {
        try {
            System.out.println("beginTime: " + FormatUtil.formatTime() );
            System.out.println("run threadName : " + this.currentThread().getName() + " begin");
            Thread.sleep(2000);
            System.out.println("run threadName : " + this.currentThread().getName() + " end");
            System.out.println("endTime: " + FormatUtil.formatTime() );
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
package com.com.demo6;

public class Run {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();
    }
}

运行的结果:

beginTime: 2018-12-04 16:55:04
run threadName : Thread-0 begin
run threadName : Thread-0 end
endTime: 2018-12-04 16:55:06

猜你喜欢

转载自blog.csdn.net/toyota_rav4/article/details/84791608
今日推荐