JAVA线程示范之一种

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

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

上一篇:利用redis + lua解决抢红包高并发的问题


下一篇:Java线程:线程的调度-守护线程