Runnable與Callable的Demo

說說Runnable與Callable

Callable接口:

public interface Callable<V> {
    V call() throws Exception;
}

 

Runnable接口:

public interface Runnable {
    public abstract void run();
}

相同點:

  1. 兩者都是接口;(廢話)
  2. 兩者都可用來編寫多線程程序;
  3. 兩者都需要調用Thread.start()啓動線程;

 

不同點:

  1. 兩者最大的不同點是:實現Callable接口的任務線程能返回執行結果;而實現Runnable接口的任務線程不能返回結果;
  2. Callable接口的call()方法允許拋出異常;而Runnable接口的run()方法的異常只能在內部消化,不能繼續上拋;

 

注意點:

  • Callable接口支持返回執行結果,此時需要調用FutureTask.get()方法實現,此方法會阻塞主線程直到獲取‘將來’結果;當不調用此方法時,主線程不會阻塞!

 

Callable工作的Demo:

 


package com.callable.runnable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Created on 2016/5/18.
 */
public class CallableImpl implements Callable<String> {

    public CallableImpl(String acceptStr) {
        this.acceptStr = acceptStr;
    }

    private String acceptStr;

    @Override
    public String call() throws Exception {
        // 任務阻塞 1 秒
        Thread.sleep(1000);
        return this.acceptStr + " append some chars and return it!";
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<String> callable = new CallableImpl("my callable test!");
        FutureTask<String> task = new FutureTask<>(callable);
        long beginTime = System.currentTimeMillis();
        // 創建線程
        new Thread(task).start();
        // 調用get()阻塞主線程,反之,線程不會阻塞
        String result = task.get();
        long endTime = System.currentTimeMillis();
        System.out.println("hello : " + result);
        System.out.println("cast : " + (endTime - beginTime) / 1000 + " second!");
    }
}

 

 

 

測試結果:

hello : my callable test! append some chars and return it!
cast : 1 second!

Process finished with exit code 0

 

 

Runnable工作的Demo:

package com.callable.runnable;

/**
 * Created on 2016/5/18.
 */
public class RunnableImpl implements Runnable {

    public RunnableImpl(String acceptStr) {
        this.acceptStr = acceptStr;
    }

    private String acceptStr;

    @Override
    public void run() {
        try {
            // 線程阻塞 1 秒,此時有異常產生,只能在方法內部消化,無法上拋
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 最終處理結果無法返回
        System.out.println("hello : " + this.acceptStr);
    }


    public static void main(String[] args) {
        Runnable runnable = new RunnableImpl("my runable test!");
        long beginTime = System.currentTimeMillis();
        new Thread(runnable).start();
        long endTime = System.currentTimeMillis();
        System.out.println("cast : " + (endTime - beginTime) / 1000 + " second!");
    }
}

 

測試結果:

cast : 0 second!
hello : my runable test!

Process finished with exit code 0

 

 

寫此篇的原因是一次面試中問到Callable與Runnable的區別,當時用的多的是Runnable,而Callable使用很少!

比較了兩者後(網上查了不少),發現Callable在很多特殊的場景下還是很有用的!最後留點抄的代碼,加深對Callable的認識!

package com.inte.fork;

/**
 * Created on 2016/4/20.
 */

import java.util.*;
import java.util.concurrent.*;

import static java.util.Arrays.asList;

public class Sums {

    static class Sum implements Callable<Long> {
        private final long from;
        private final long to;

        Sum(long from, long to) {
            this.from = from;
            this.to = to;
        }

        @Override
        public Long call() {
            long acc = 0;
            for (long i = from; i <= to; i++) {
                acc = acc + i;
            }
            System.out.println(Thread.currentThread().getName() + " : " + acc);
            return acc;
        }
    }

    public static void main(String[] args) throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        List<Future<Long>> results = executor.invokeAll(asList(
                new Sum(0, 10), new Sum(0, 1_000), new Sum(0, 1_000_000)
        ));
        executor.shutdown();

        for (Future<Long> result : results) {
            System.out.println(result.get());
        }
    }
}

轉自 https://www.cnblogs.com/frinder6/p/5507082.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章