Runnable
@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
@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; }
Same point:
1. All interfaces can realize multithreading programming, and Thread.start() is required to start threads.
Difference:
1. The callable interface supports the return of execution results. At this time, the FutureTask.get() method needs to be called for implementation. This method will block the main thread until the 'future' result is obtained. When this method is not called, the main thread will not block!
Callable use
/** * CallableImpl Brief description * <p> TODO:Describe such responsibilities</p> * * @author ckmike * @version 1.0 * @date 18-12-6 8:53 p.m. * @copyright ckmike **/ public class CallableImpl implements Callable<String> { private String accept; public CallableImpl(String accept) { this.accept = accept; } @Override public String call() throws Exception { // dormancy TimeUnit.SECONDS.sleep(3); return this.accept; } 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(); // Create thread new Thread(task).start(); // Call get() to block the main thread, otherwise, the thread will not block String result = task.get(); long endTime = System.currentTimeMillis(); System.out.println("hello : " + result); System.out.println("cast : " + (endTime - beginTime) / 1000 + " second!"); } }
Runnable use
/** * RunnableImpl Brief description * <p> TODO:Describe such responsibilities</p> * * @author ckmike * @version 1.0 * @date 18-12-6 9:03 p.m. * @copyright ckmike **/ public class RunnableImpl implements Runnable { public RunnableImpl(String accept) { this.accept = accept; } private String accept; @Override public void run() { // The thread is blocked for one second. At this time, an exception is generated. It can only be digested inside the method and cannot be thrown up try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } // Final processing result cannot be returned System.out.println("hello : " + this.accept); } 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!"); } }
The reason why I wrote this article is: when I asked about the difference between Runnable and Callable during the interview, I usually used Runnable more often than Callable. I didn't use Callable basically, so I hung up, hahaha, and summarized it to my friends in need. Do you know the use scenario of the Callable interface? Can you share it with me? Thank you!