Java併發編程 —— 淺談Runnable和Callable

Runnable接口:java

@FunctionalInterface
public interface Runnable {
    /** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see java.lang.Thread#run() */
    public abstract void run();
}
複製代碼

Callable接口:app

@FunctionalInterface
public interface Callable<V> {
    /** * Computes a result, or throws an exception if unable to do so. * * @return computed result * @throws Exception if unable to compute a result */
    V call() throws Exception;
}
複製代碼

異同

相同點

  1. 都是接口(=-=|||)
  2. 都須要thread.start()來啓動線程

不一樣點

  1. 從接口源碼註釋能夠看出,Runnable規定的方法是run(),Callable規定的方法是call()
  2. Callable執行後能夠返回值,Runnable無返回值
  3. call()方法能夠throws Exception,run()不能

Callable接口的實現類能夠經過一個FutureTask.get()來獲取call()中的返回值,此方法會阻塞線程知道得到「未來」的結果,不調用此方法,主線程不會阻塞。ide


最後附上兩段抄來的demothis

Callable工做demo:spa

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!");
    }
}
複製代碼

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!");
    }
}

複製代碼
相關文章
相關標籤/搜索