标签:调用 ted exit local trace utf-8 说明 catch string
方法sleep()的作用是指在指定的毫秒数内让当前正在执行的线程休眠(暂停执行)这个正在执行的线程是指this.currentThread()返回的线程。
测试如下
1 package com.cky.thread; 2 3 /** 4 * Created by edison on 2017/11/28. 5 */ 6 public class MyThread10 extends Thread{ 7 @Override 8 public void run() { 9 try { 10 super.run(); 11 System.out.println("run threadname "+this.currentThread().getName()+" begin"); 12 Thread.sleep(2000); 13 System.out.println("run threadname "+this.currentThread().getName()+" end"); 14 } catch (InterruptedException e) { 15 e.printStackTrace(); 16 } 17 } 18 }
1 package com.cky.test; 2 3 import com.cky.thread.MyThread10; 4 5 /** 6 * Created by edison on 2017/11/28. 7 */ 8 public class Test16 { 9 public static void main(String[] args) { 10 MyThread10 th = new MyThread10(); 11 System.out.println("begin="+System.currentTimeMillis()); 12 th.run(); 13 System.out.println("end= "+System.currentTimeMillis()); 14 } 15 }
1 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 2 begin=1512280757440 3 run threadname main begin 4 run threadname main end 5 end= 1512280759446 6 7 Process finished with exit code 0
结果分析:
直接调用run方法,说明此时并没有开启子线程,run函数里的方法都是主线程调用的,所以当线程睡眠也是主线程睡眠,代码顺序依次执行,中间最后输出的开始和结束时间相隔在2秒多的样子。
这时将调用run改成start();
1 package com.cky.test; 2 3 import com.cky.thread.MyThread10; 4 5 /** 6 * Created by edison on 2017/11/28. 7 */ 8 public class Test16 { 9 public static void main(String[] args) { 10 MyThread10 th = new MyThread10(); 11 System.out.println("begin="+System.currentTimeMillis()); 12 th.start(); 13 System.out.println("end= "+System.currentTimeMillis()); 14 } 15 }
1 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 2 begin=1512281012995 3 end= 1512281012995 4 run threadname Thread-0 begin 5 run threadname Thread-0 end 6 7 Process finished with exit code 0
结果分析:
使用start()方法启动线程,由于这是cpu还是切换到了主线程,main线程与子线程是异步执行的,所以,执行完了main线程,这时再执行子线程。
标签:调用 ted exit local trace utf-8 说明 catch string
原文地址:http://www.cnblogs.com/edison20161121/p/7954654.html