2018-08-13 15:33:27 1177瀏覽
今天扣丁學(xué)堂Java培訓(xùn)老師給大家介紹一下關(guān)于Java開發(fā)多線程之Callable接口的實現(xiàn),Callable和Runnbale一樣代表著任務(wù),區(qū)別在于Callable有返回值并且可以拋出異常,下面我們一起來看一下吧。
public interface Callable<V> { V call() throws Exception; }
public interface Runnable { public abstract void run(); }
class MyCallableTask implements Callable<Integer>{ @Override public Integer call() throws Exception { System.out.println("線程在進(jìn)行計算"); Thread.sleep(3000); int sum = 0; for(int i=0;i<100;i++) sum += i; return sum; } }
Callable<Integer> mycallabletask = new MyCallableTask(); FutureTask<Integer> futuretask= new FutureTask<Integer>(mycallabletask); new Thread(futuretask).start();
public interface ExecutorService extends Executor { //提交一個Callable任務(wù),返回值為一個Future類型 <T> Future<T> submit(Callable<T> task); //other methods... }
ExecutorService exec = Executors.newCachedThreadPool(); Future<Integer> future = exec.submit(new MyCallableTask());
public class CallableAndFutureTask { public static void main(String[] args) { Callable<Integer> callable = new Callable<Integer>() { public Integer call() throws Exception { return new Random().nextInt(100); } }; FutureTask<Integer> future = new FutureTask<Integer>(callable); new Thread(future).start(); try { Thread.sleep(5000); System.out.println(future.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }
public class CallableAndFuture { public static void main(String[] args) { ExecutorService threadPool = Executors.newSingleThreadExecutor(); Future<Integer> future = threadPool.submit(new Callable<Integer>() { public Integer call() throws Exception { return new Random().nextInt(100); } }); try { Thread.sleep(5000);// 可能做一些事情 System.out.println(future.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }
class MyCallableTask implements Callable<String> { private int id; public OneTask(int id){ this.id = id; } @Override public String call() throws Exception { for(int i = 0;i<5;i++){ System.out.println("Thread"+ id); Thread.sleep(1000); } return "Result of callable: "+id; } } public class Test { public static void main(String[] args) { //Callable<String> mycallabletask = new MyCallableTask(1); ExecutorService exec = Executors.newCachedThreadPool(); ArrayList<Future<String>> results = new ArrayList<Future<String>>(); for (int i = 0; i < 5; i++) { results.add(exec.submit(new MyCallableTask(i))); } for (Future<String> fs : results) { if (fs.isDone()) { try { System.out.println(fs.get()); } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("MyCallableTask任務(wù)未完成!"); } } exec.shutdown(); } }
以上就是扣丁學(xué)堂Java培訓(xùn)之多線程實現(xiàn)Callable接口代碼示例的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,想要了解更多關(guān)于Java開發(fā)內(nèi)容的小伙伴可以登錄扣丁學(xué)堂官網(wǎng)咨詢,扣丁學(xué)堂Java視頻教程讓學(xué)員免費觀看學(xué)習(xí),扣丁學(xué)堂Java技術(shù)交流群:670348138。
【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】
查看更多關(guān)于“Java開發(fā)資訊”的相關(guān)文章>>