avatarEray Araz

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1794

Abstract

span> <span class="hljs-title class_">MyCallable</span>(); FutureTask<Integer> futureTask = <span class="hljs-keyword">new</span> <span class="hljs-title class_">FutureTask</span><>(callable); <span class="hljs-type">Thread</span> <span class="hljs-variable">thread</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Thread</span>(futureTask); thread.start(); <span class="hljs-type">Integer</span> <span class="hljs-variable">result</span> <span class="hljs-operator">=</span> futureTask.get(); System.out.println(<span class="hljs-string">"Result: "</span> + result); } }</pre></div><p id="9f76">Output :</p><div id="286e"><pre><span class="hljs-section">Result: 42</span></pre></div><h2 id="6afc">What is Runnable?</h2><p id="5923"><code>Runnable</code> is an older interface in Java and is often used in single-threaded applications. <code>Runnable</code> defines a single method, <code>run()</code>, which sequentially performs tasks.</p><div id="439f"><pre><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyRunnable</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Runnable</span> { <span class="hljs-meta">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-built_in">void</span> <span class="hljs-title function_">run</span>(<span class="hljs-params"></span>) { <span class="hljs-comment">// Perform your tasks here</span> <span class="hljs-title class_">System</span>.<span class="hljs-property">out</span>.<span class="hljs-title function_">println</span>(<span class="hljs-string">"Hello, Runnable!"</span>); }

<span class="hljs-keywo

Options

rd">public</span> <span class="hljs-keyword">static</span> <span class="hljs-built_in">void</span> <span class="hljs-title function_">main</span>(<span class="hljs-params"><span class="hljs-built_in">String</span>[] args</span>) { <span class="hljs-title class_">Runnable</span> runnable = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyRunnable</span>(); <span class="hljs-title class_">Thread</span> thread = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Thread</span>(runnable); thread.<span class="hljs-title function_">start</span>(); } }</pre></div><h2 id="600e">Differences between Callable and Runnable</h2><ul><li>The <code>call()</code> method of <code>Callable</code> can return a value, while the <code>run()</code> method of <code>Runnable</code> cannot return a value.</li><li>When working with <code>Callable</code>, you can use mechanisms like <code>Future</code> or <code>FutureTask</code> to obtain the result when the task is completed. <code>Runnable</code> does not provide such a mechanism directly.</li><li><code>Callable</code> can specify checked exceptions (e.g., <code>Exception</code>) when throwing exceptions, while <code>Runnable</code> cannot throw checked exceptions.</li><li><code>Callable</code> returns a result when the thread's work is completed, while <code>Runnable</code> only completes the work, requiring a separate mechanism to transmit the result.</li></ul><p id="3359">The choice between which interface to use depends on the requirements of your project and the use of multi-threading. If you need to return a result or handle exceptions, <code>Callable</code> may be more appropriate. If you simply want to run a task in parallel, <code>Runnable</code> can be used.</p></article></body>

What is a Callable in Java, and what are the differences between Callable and Runnable?

What is Callable?

Callable is an interface in Java found in the java.util.concurrent package. It is often used in multi-threading environments. Callable defines a single method, call(), which returns a value that can be obtained through Future<V> objects.

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

public class MyCallable implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        // Perform your tasks here and return the result
        return 42;
    }


public static void main(String[] args) throws Exception {
    Callable<Integer> callable = new MyCallable();
    FutureTask<Integer> futureTask = new FutureTask<>(callable);
    Thread thread = new Thread(futureTask);
    thread.start();
    Integer result = futureTask.get();
    System.out.println("Result: " + result);
}
}

Output :

Result: 42

What is Runnable?

Runnable is an older interface in Java and is often used in single-threaded applications. Runnable defines a single method, run(), which sequentially performs tasks.

public class MyRunnable implements Runnable {
    @Override
    public void run() {
        // Perform your tasks here
        System.out.println("Hello, Runnable!");
    }


public static void main(String[] args) {
    Runnable runnable = new MyRunnable();
    Thread thread = new Thread(runnable);
    thread.start();
}
}

Differences between Callable and Runnable

  • The call() method of Callable can return a value, while the run() method of Runnable cannot return a value.
  • When working with Callable, you can use mechanisms like Future or FutureTask to obtain the result when the task is completed. Runnable does not provide such a mechanism directly.
  • Callable can specify checked exceptions (e.g., Exception) when throwing exceptions, while Runnable cannot throw checked exceptions.
  • Callable returns a result when the thread's work is completed, while Runnable only completes the work, requiring a separate mechanism to transmit the result.

The choice between which interface to use depends on the requirements of your project and the use of multi-threading. If you need to return a result or handle exceptions, Callable may be more appropriate. If you simply want to run a task in parallel, Runnable can be used.

Recommended from ReadMedium