线程使用 sleep(1000)和 join(1000)在运行上没有什么区别,比如下面代码
public class Run {
public static void main(String[] args) {
try{
MyThread mt = new MyThread();
mt.start();
//mt.join(2000);
mt.sleep(2000);
System.out.println("end timer = "+System.currentTimeMillis());
}catch(InterruptedException e){
e.printStackTrace();
}
}
}public class MyThread extends Thread{
public void run(){
try{
System.out.println("begin Timer ="+ System.currentTimeMillis());
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}从源码中可以了解其内部的区别如下:1:join(long)方法在内部使用的是 wait (long) 方法来实现的;所以join(long)方法具有释放锁的特点
2:sleep (long) 方法具有不释放锁的特性
版权声明:本文为lisi1129原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。