方法sleep()的作用是指在指定的毫秒数内让当前正在执行的线程休眠(暂停执行)这个正在执行的线程是指this.currentThread()返回的线程。
测试如下
package com.cky.thread; /**
* Created by edison on 2017/11/28.
*/
public class MyThread10 extends Thread{
@Override
public void run() {
try {
super.run();
System.out.println("run threadname "+this.currentThread().getName()+" begin");
Thread.sleep(2000);
System.out.println("run threadname "+this.currentThread().getName()+" end");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.cky.test; import com.cky.thread.MyThread10; /**
* Created by edison on 2017/11/28.
*/
public class Test16 {
public static void main(String[] args) {
MyThread10 th = new MyThread10();
System.out.println("begin="+System.currentTimeMillis());
th.run();
System.out.println("end= "+System.currentTimeMillis());
}
}
C:\itsoft\jdk\bin\java -Didea.launcher.port=7541 "-Didea.launcher.bin.path=C:\itsoft\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\itsoft\jdk\jre\lib\charsets.jar;C:\itsoft\jdk\jre\lib\deploy.jar;C:\itsoft\jdk\jre\lib\ext\access-bridge-32.jar;C:\itsoft\jdk\jre\lib\ext\cldrdata.jar;C:\itsoft\jdk\jre\lib\ext\dnsns.jar;C:\itsoft\jdk\jre\lib\ext\jaccess.jar;C:\itsoft\jdk\jre\lib\ext\jfxrt.jar;C:\itsoft\jdk\jre\lib\ext\localedata.jar;C:\itsoft\jdk\jre\lib\ext\nashorn.jar;C:\itsoft\jdk\jre\lib\ext\sunec.jar;C:\itsoft\jdk\jre\lib\ext\sunjce_provider.jar;C:\itsoft\jdk\jre\lib\ext\sunmscapi.jar;C:\itsoft\jdk\jre\lib\ext\sunpkcs11.jar;C:\itsoft\jdk\jre\lib\ext\zipfs.jar;C:\itsoft\jdk\jre\lib\javaws.jar;C:\itsoft\jdk\jre\lib\jce.jar;C:\itsoft\jdk\jre\lib\jfr.jar;C:\itsoft\jdk\jre\lib\jfxswt.jar;C:\itsoft\jdk\jre\lib\jsse.jar;C:\itsoft\jdk\jre\lib\management-agent.jar;C:\itsoft\jdk\jre\lib\plugin.jar;C:\itsoft\jdk\jre\lib\resources.jar;C:\itsoft\jdk\jre\lib\rt.jar;C:\多线程核心技术\第一章\out\production\第一章;C:\itsoft\idea\IntelliJ IDEA 2016.3.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test16
begin=1512280757440
run threadname main begin
run threadname main end
end= 1512280759446 Process finished with exit code 0
结果分析:
直接调用run方法,说明此时并没有开启子线程,run函数里的方法都是主线程调用的,所以当线程睡眠也是主线程睡眠,代码顺序依次执行,中间最后输出的开始和结束时间相隔在2秒多的样子。
这时将调用run改成start();
package com.cky.test; import com.cky.thread.MyThread10; /**
* Created by edison on 2017/11/28.
*/
public class Test16 {
public static void main(String[] args) {
MyThread10 th = new MyThread10();
System.out.println("begin="+System.currentTimeMillis());
th.start();
System.out.println("end= "+System.currentTimeMillis());
}
}
C:\itsoft\jdk\bin\java -Didea.launcher.port=7540 "-Didea.launcher.bin.path=C:\itsoft\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\itsoft\jdk\jre\lib\charsets.jar;C:\itsoft\jdk\jre\lib\deploy.jar;C:\itsoft\jdk\jre\lib\ext\access-bridge-32.jar;C:\itsoft\jdk\jre\lib\ext\cldrdata.jar;C:\itsoft\jdk\jre\lib\ext\dnsns.jar;C:\itsoft\jdk\jre\lib\ext\jaccess.jar;C:\itsoft\jdk\jre\lib\ext\jfxrt.jar;C:\itsoft\jdk\jre\lib\ext\localedata.jar;C:\itsoft\jdk\jre\lib\ext\nashorn.jar;C:\itsoft\jdk\jre\lib\ext\sunec.jar;C:\itsoft\jdk\jre\lib\ext\sunjce_provider.jar;C:\itsoft\jdk\jre\lib\ext\sunmscapi.jar;C:\itsoft\jdk\jre\lib\ext\sunpkcs11.jar;C:\itsoft\jdk\jre\lib\ext\zipfs.jar;C:\itsoft\jdk\jre\lib\javaws.jar;C:\itsoft\jdk\jre\lib\jce.jar;C:\itsoft\jdk\jre\lib\jfr.jar;C:\itsoft\jdk\jre\lib\jfxswt.jar;C:\itsoft\jdk\jre\lib\jsse.jar;C:\itsoft\jdk\jre\lib\management-agent.jar;C:\itsoft\jdk\jre\lib\plugin.jar;C:\itsoft\jdk\jre\lib\resources.jar;C:\itsoft\jdk\jre\lib\rt.jar;C:\多线程核心技术\第一章\out\production\第一章;C:\itsoft\idea\IntelliJ IDEA 2016.3.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test16
begin=1512281012995
end= 1512281012995
run threadname Thread-0 begin
run threadname Thread-0 end Process finished with exit code 0
结果分析:
使用start()方法启动线程,由于这是cpu还是切换到了主线程,main线程与子线程是异步执行的,所以,执行完了main线程,这时再执行子线程。