public class ThreadDemo extends Thread { public static void main(String[] args) { ThreadDemo thread = new ThreadDemo(); thread.start(); } @Override public void run() { System.out.println("這是Thread方式啓動的"); } }
運行結果以下:
ide
public class RunnableDemo implements Runnable { @Override public void run() { System.out.println("這是Runnable方式啓動的"); } public static void main(String[] args) { Thread thread = new Thread(new RunnableDemo()); thread.start(); } }
運行結果以下:
spa
public class CallableDemo implements Callable { @Override public String call() throws Exception { return "Callable"; } public static void main(String[] args) { FutureTask<String> futureTask = new FutureTask(new CallableDemo()); Thread thread = new Thread(futureTask); thread.start(); try { System.out.println(futureTask.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }
運行結果以下:
code