码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA线程示范之一种

时间:2015-05-10 12:43:43      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

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

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

JAVA线程示范之一种

标签:

原文地址:http://www.cnblogs.com/aguncn/p/4492010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!