zoukankan      html  css  js  c++  java
  • 1.5sleep()方法

    方法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:itsoftjdkinjava -Didea.launcher.port=7541 "-Didea.launcher.bin.path=C:itsoftideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "C:itsoftjdkjrelibcharsets.jar;C:itsoftjdkjrelibdeploy.jar;C:itsoftjdkjrelibextaccess-bridge-32.jar;C:itsoftjdkjrelibextcldrdata.jar;C:itsoftjdkjrelibextdnsns.jar;C:itsoftjdkjrelibextjaccess.jar;C:itsoftjdkjrelibextjfxrt.jar;C:itsoftjdkjrelibextlocaledata.jar;C:itsoftjdkjrelibext
    ashorn.jar;C:itsoftjdkjrelibextsunec.jar;C:itsoftjdkjrelibextsunjce_provider.jar;C:itsoftjdkjrelibextsunmscapi.jar;C:itsoftjdkjrelibextsunpkcs11.jar;C:itsoftjdkjrelibextzipfs.jar;C:itsoftjdkjrelibjavaws.jar;C:itsoftjdkjrelibjce.jar;C:itsoftjdkjrelibjfr.jar;C:itsoftjdkjrelibjfxswt.jar;C:itsoftjdkjrelibjsse.jar;C:itsoftjdkjrelibmanagement-agent.jar;C:itsoftjdkjrelibplugin.jar;C:itsoftjdkjrelib
    esources.jar;C:itsoftjdkjrelib
    t.jar;C:多线程核心技术第一章outproduction第一章;C:itsoftideaIntelliJ IDEA 2016.3.3libidea_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:itsoftjdkinjava -Didea.launcher.port=7540 "-Didea.launcher.bin.path=C:itsoftideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "C:itsoftjdkjrelibcharsets.jar;C:itsoftjdkjrelibdeploy.jar;C:itsoftjdkjrelibextaccess-bridge-32.jar;C:itsoftjdkjrelibextcldrdata.jar;C:itsoftjdkjrelibextdnsns.jar;C:itsoftjdkjrelibextjaccess.jar;C:itsoftjdkjrelibextjfxrt.jar;C:itsoftjdkjrelibextlocaledata.jar;C:itsoftjdkjrelibext
    ashorn.jar;C:itsoftjdkjrelibextsunec.jar;C:itsoftjdkjrelibextsunjce_provider.jar;C:itsoftjdkjrelibextsunmscapi.jar;C:itsoftjdkjrelibextsunpkcs11.jar;C:itsoftjdkjrelibextzipfs.jar;C:itsoftjdkjrelibjavaws.jar;C:itsoftjdkjrelibjce.jar;C:itsoftjdkjrelibjfr.jar;C:itsoftjdkjrelibjfxswt.jar;C:itsoftjdkjrelibjsse.jar;C:itsoftjdkjrelibmanagement-agent.jar;C:itsoftjdkjrelibplugin.jar;C:itsoftjdkjrelib
    esources.jar;C:itsoftjdkjrelib
    t.jar;C:多线程核心技术第一章outproduction第一章;C:itsoftideaIntelliJ IDEA 2016.3.3libidea_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线程,这时再执行子线程。

  • 相关阅读:
    python json 和 pickle的补充 hashlib configparser logging
    go 流程语句 if goto for swich
    go array slice map make new操作
    go 基础
    块级元素 行内元素 空元素
    咽炎就医用药(慢性肥厚性咽炎)
    春季感冒是风寒还是风热(转的文章)
    秋季感冒 咳嗽 怎么选药
    解决IE浏览器“无法显示此网页”的问题
    常用的 css 样式 记录
  • 原文地址:https://www.cnblogs.com/edison20161121/p/7954654.html
Copyright © 2011-2022 走看看