use sleep() to gain thread predictability

来源:互联网 发布:淘宝客怎样推广店铺 编辑:程序博客网 时间:2024/04/28 16:45
package HeadFirstJava;class MyRunnable implements Runnable {    public void run() {        go();    }    public void go() {        try {            Thread.sleep(2000);        } catch(InterruptedException ex) {            ex.printStackTrace();        }        doMore();    }    public void doMore() {        System.out.println("top of the stack");    }}class ThreadTestDrive {    public static void main(String[] args) {        MyRunnable theJob = new MyRunnable();        Thread t = new Thread(theJob);        t.start();        System.out.println("back in main");    }}

back in main

top of the stack

// notice the time pause between 'back in main' & 'top of the stack',

// which is about 2 seconds

0 0
原创粉丝点击