Callable 有傳回值的非同步(Runnable 非同步是沒有傳回值的)__Java基礎

來源:互聯網
上載者:User

Callable 和 Runnable 的使用方法大同小異, 區別在於: 

1、Callable 使用 call() 方法, Runnable 使用 run() 方法 
2、call() 有傳回值, 而 run()方法沒有傳回值。 
3、call() 可以拋出受檢查的異常,比如ClassNotFoundException, 而run()不能拋出受檢查的異常。 


Callable樣本如下: 

class CallableImpl implements Callable {private int age;public CallableImpl(int age) {this.age = age;}// 具體邏輯在這裡實現,並有一個傳回值public Object call() throws Exception {Thread.currentThread().sleep(new Random().nextInt(1000));String ss="";if (age < 0) {ss="age = " + age + ", 輸入年齡有誤";} else if (age < 20) {ss="age = " + age + ", 你好,年輕人";} else if (age < 40) {ss= "age = " + age + ", 你好,中年人";} else {ss= "age = " + age + ", 你好,老年人";}System.out.println(ss);return ss;}}public class CallableTest {public static void main(String[] args) throws InterruptedException, ExecutionException {// 建立一個線程池ExecutorService exc = Executors.newFixedThreadPool(3);// 存放返回的結果List<Future<String>> futures = new ArrayList<Future<String>>();for (int i = -10; i < 50; i += 3) {CallableImpl call = new CallableImpl(i);// 執行callable的call方法,並有傳回值Future<String> future = exc.submit(call);futures.add(future);}System.out.println("等待結果:");for (Future<String> future : futures) {              // 這裡調用get()方法會阻塞當前線程,直到得到返回結果              System.out.println("----------- "+future.get());              // if (future.isDone()) {              // System.out.println(future.get());              // } else {              // System.out.println("Future result is not yet complete");              // }          }System.out.println("結束...");//Thread.currentThread().sleep(1000*30);exc.shutdown();}}

結果:

等待結果:age = -10, 輸入年齡有誤----------- age = -10, 輸入年齡有誤age = -4, 輸入年齡有誤age = -1, 輸入年齡有誤age = 2, 你好,年輕人age = -7, 輸入年齡有誤----------- age = -7, 輸入年齡有誤----------- age = -4, 輸入年齡有誤----------- age = -1, 輸入年齡有誤----------- age = 2, 你好,年輕人age = 11, 你好,年輕人age = 5, 你好,年輕人----------- age = 5, 你好,年輕人age = 8, 你好,年輕人----------- age = 8, 你好,年輕人----------- age = 11, 你好,年輕人age = 17, 你好,年輕人age = 14, 你好,年輕人----------- age = 14, 你好,年輕人----------- age = 17, 你好,年輕人age = 26, 你好,中年人age = 23, 你好,中年人age = 20, 你好,中年人----------- age = 20, 你好,中年人----------- age = 23, 你好,中年人----------- age = 26, 你好,中年人age = 35, 你好,中年人age = 32, 你好,中年人age = 41, 你好,老年人age = 29, 你好,中年人----------- age = 29, 你好,中年人----------- age = 32, 你好,中年人----------- age = 35, 你好,中年人age = 38, 你好,中年人----------- age = 38, 你好,中年人----------- age = 41, 你好,老年人age = 44, 你好,老年人----------- age = 44, 你好,老年人age = 47, 你好,老年人----------- age = 47, 你好,老年人結束...


Runnable樣本:

public class LiftOff implements Runnable {protected int countDown = 10;private static int taskCount = 0;private final int id = taskCount++;public LiftOff() {}public LiftOff(int countDown) {this.countDown = countDown;}public String status() {return "#" + id + "(" + (countDown > 0 ? countDown : "LiftOff! ") + ")";}@Overridepublic void run() {while (countDown-- > 0) {System.out.print(status());Thread.yield();}System.out.println();}public static void main(String[] args) {ExecutorService exec = Executors.newFixedThreadPool(1);for (int i = 0; i < 5; i++) {exec.execute(new LiftOff());}exec.shutdown();}}


注意ExecutorService 在Callable中使用的是submit(), 在Runnable中使用的是 execute()。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.