Thread.sleep()和Thread.currentThread().sleep()区别

来源:互联网 发布:关于网络文学社的建议 编辑:程序博客网 时间:2024/05/15 10:27

线程可以用继承Thread类或者实现Runnable接口来实现.

Thread.sleep()是Thread类的方法,只对当前线程起作用,睡眠一段时间.

如果线程是通过继承Thread实现的话这2个方法没有区别;

如果线程是通过实现Runnable接口来实现的,则不是Thread类,不能直接使用Thread.sleep()

必须使用Thread.currentThread()来得到当前线程的引用才可以调用sleep(),

所以要用Thread.currentThread().sleep()来睡眠...

0 0