线程有两种方法生成,这是其中的一种。。
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