JAVA线程示范之一种

时间:2015-05-10 12:43:43   收藏:0   阅读:109

线程有两种方法生成,这是其中的一种。。

MyRunnable.java

public 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 statck");
    }
}

 

ThreadTester.java

public class ThreadTester {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Runnable threadJob = new MyRunnable();
        Thread myThread = new Thread(threadJob);
        
        myThread.start();
        try {
            Thread.sleep(4000);
        } catch(InterruptedException ex) {
            ex.printStackTrace();
        }
        System.out.println("Back in main");

    }
    

}

输出:

top of the statck
Back in main

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!