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

来源:互联网 发布:anime studio mac 编辑:程序博客网 时间:2024/04/28 18:07

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

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

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

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

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

原创粉丝点击