三種線程創建方式
阿新 • • 發佈:2019-03-03
adc get() 代碼 三種 實現類 接口 inter 創建 trac
先看一段代碼
import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; class ThreadA extends Thread { @Override public void run() { System.out.println("繼承自Thread類的ThreadA"); } } class ThreadB implementsRunnable { @Override public void run() { // TODO Auto-generated method stub System.out.println("實現Runnable接口的ThreadB"); } } class ThreadC implements Callable<String> { @Override public String call() throws Exception { // TODO Auto-generated method stubreturn "實現類Callable接口的ThreadC"; } } public class CreateThread { public static void main(String[] args) { new Thread(new ThreadA()).start(); new Thread(new ThreadB()).start(); ThreadC threadC = new ThreadC(); FutureTask<String> futureTask = newFutureTask<String>(threadC) ; new Thread(futureTask).start(); try { System.out.println(futureTask.get()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
待編輯
三種線程創建方式