Java Callable Future Example(java 关于Callable,Future的例子)

Java Callable Future Example

Java Callable and Future are used a lot in multithreaded programming. In last few posts, we learned a lot about java threads but sometimes we wish that a thread could return some value that we can use. Java 5 introduced java.util.concurrent.Callable interface in concurrency package that is similar to Runnable interface but it can return any Object and able to throw Exception.

Java Callable

java Callable, java Future, java callable example, java executorservice callable
Java Callable interface use Generic to define the return type of Object. Executors class provide useful methods to execute Java Callable in a thread pool. Since callable tasks run in parallel, we have to wait for the returned Object.

Java Future

Java Callable tasks return java.util.concurrent.Future object. Using Java Future object, we can find out the status of the Callable task and get the returned Object. It provides get() method that can wait for the Callable to finish and then return the result.

Java Future provides cancel() method to cancel the associated Callable task. There is an overloaded version of get() method where we can specify the time to wait for the result, it’s useful to avoid current thread getting blocked for longer time. There are isDone() and isCancelled() methods to find out the current status of associated Callable task.

Here is a simple example of Java Callable task that returns the name of thread executing the task after one second. We are using Executor framework to execute 100 tasks in parallel and use Java Future to get the result of the submitted tasks.

Copy
package com.journaldev.threads; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class MyCallable implements Callable<String> { @Override public String call() throws Exception { Thread.sleep(1000); //return the thread name executing this callable task return Thread.currentThread().getName(); } public static void main(String args[]){ //Get ExecutorService from Executors utility class, thread pool size is 10 ExecutorService executor = Executors.newFixedThreadPool(10); //create a list to hold the Future object associated with Callable List<Future<String>> list = new ArrayList<Future<String>>(); //Create MyCallable instance Callable<String> callable = new MyCallable(); for(int i=0; i< 100; i++){ //submit Callable tasks to be executed by thread pool Future<String> future = executor.submit(callable); //add Future to the list, we can get return value using Future list.add(future); } for(Future<String> fut : list){ try { //print the return value of Future, notice the output delay in console // because Future.get() waits for task to get completed System.out.println(new Date()+ "::"+fut.get()); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } } //shut down the executor service now executor.shutdown(); } }

Once we execute the above program, you will notice the delay in output because java Future get() method waits for the java callable task to complete. Also notice that there are only 10 threads executing these tasks.

Here is snippet of the output of above program.

Copy
Mon Dec 31 20:40:15 PST 2012::pool-1-thread-1 Mon Dec 31 20:40:16 PST 2012::pool-1-thread-2 Mon Dec 31 20:40:16 PST 2012::pool-1-thread-3 Mon Dec 31 20:40:16 PST 2012::pool-1-thread-4 Mon Dec 31 20:40:16 PST 2012::pool-1-thread-5 Mon Dec 31 20:40:16 PST 2012::pool-1-thread-6 Mon Dec 31 20:40:16 PST 2012::pool-1-thread-7 Mon Dec 31 20:40:16 PST 2012::pool-1-thread-8 Mon Dec 31 20:40:16 PST 2012::pool-1-thread-9 Mon Dec 31 20:40:16 PST 2012::pool-1-thread-10 Mon Dec 31 20:40:16 PST 2012::pool-1-thread-2 ...

Tip: What if we want to override some of the methods of Java Future interface, for example overriding get() method to timeout after some default time rather than waiting indefinitely, in this case Java FutureTask class comes handy that is the base implementation of Future interface. Check out Java FutureTask Example to learn more about this class.

Comments

  1. 	<header class="comment-header">
    		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
    			<span itemprop="name">Aakshi</span> <span class="says">says</span>			</p>
    
    		<p class="comment-meta"><time class="comment-time" datetime="2018-02-15T02:28:05+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-42249" class="comment-time-link" itemprop="url">February 15, 2018 at 2:28 am</a></time></p>		</header>
    
    	<div class="comment-content" itemprop="text">
    		
    		<p>Thanks for the example.</p>
    	</div>
    
    	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-42249" onclick="return addComment.moveForm( &quot;comment-42249&quot;, &quot;42249&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Aakshi">Reply</a></div>
    	
    </article>
    </li><!-- #comment-## -->
    
    <li class="comment odd alt thread-odd thread-alt depth-1" id="comment-39722">
    <article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">
    
    	
    	<header class="comment-header">
    		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
    			<span itemprop="name">Shane</span> <span class="says">says</span>			</p>
    
    		<p class="comment-meta"><time class="comment-time" datetime="2017-10-13T15:00:30+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-39722" class="comment-time-link" itemprop="url">October 13, 2017 at 3:00 pm</a></time></p>		</header>
    
    	<div class="comment-content" itemprop="text">
    		
    		<p>Thanks for this tutorial!   I did have to make one modification.</p>
    

    I changed (A) to (B) where:

    (A) Future future = executor.submit(callable);

    (B) Future future = executor.submit(new MyCallable());

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-39722" onclick="return addComment.moveForm( &quot;comment-39722&quot;, &quot;39722&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Shane">Reply</a></div>
	
</article>
</li><!-- #comment-## -->

<li class="comment even thread-even depth-1" id="comment-38611">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">Utpal</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2017-07-10T03:31:21+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-38611" class="comment-time-link" itemprop="url">July 10, 2017 at 3:31 am</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>Thanks for sharing simple and understandable example.</p>

I have a question, it may be very silly but i want to clarify it with you.
In above example we will always get output as : pool-1-thread-(Number), where pool-1 is common, here my question is
since we have created pool of size 5. Output should also change accordingly like pool-1, pool-2 etc.

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-38611" onclick="return addComment.moveForm( &quot;comment-38611&quot;, &quot;38611&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Utpal">Reply</a></div>
	
</article>
<ul class="children">

<li class="comment byuser comment-author-pankaj bypostauthor odd alt depth-2" id="comment-38618">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name"><a href="https://www.journaldev.com" class="comment-author-link" rel="external nofollow" itemprop="url">Pankaj</a></span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2017-07-11T03:48:32+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-38618" class="comment-time-link" itemprop="url">July 11, 2017 at 3:48 am</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>We are creating 1 pool with 10 threads, that’s why the output.</p>
	</div>

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-38618" onclick="return addComment.moveForm( &quot;comment-38618&quot;, &quot;38618&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Pankaj">Reply</a></div>
	
</article>
</li><!-- #comment-## -->
<li class="comment even thread-odd thread-alt depth-1" id="comment-36669">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">Veranga Sooriyabandara</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2016-10-24T01:21:39+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-36669" class="comment-time-link" itemprop="url">October 24, 2016 at 1:21 am</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>Hi Pankaj,</p>

This is very useful. Thanks fro sharing your knowledge with us.

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-36669" onclick="return addComment.moveForm( &quot;comment-36669&quot;, &quot;36669&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Veranga Sooriyabandara">Reply</a></div>
	
</article>
</li><!-- #comment-## -->

<li class="comment odd alt thread-even depth-1" id="comment-33631">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">shgy</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2015-12-07T01:48:54+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-33631" class="comment-time-link" itemprop="url">December 7, 2015 at 1:48 am</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>Thanks ! , useful…!</p>
	</div>

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-33631" onclick="return addComment.moveForm( &quot;comment-33631&quot;, &quot;33631&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to shgy">Reply</a></div>
	
</article>
</li><!-- #comment-## -->

<li class="comment even thread-odd thread-alt depth-1" id="comment-33534">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">Chaitanya</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2015-11-18T01:58:46+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-33534" class="comment-time-link" itemprop="url">November 18, 2015 at 1:58 am</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>Super note.</p>

Thanks for sharing the stuff

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-33534" onclick="return addComment.moveForm( &quot;comment-33534&quot;, &quot;33534&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Chaitanya">Reply</a></div>
	
</article>
</li><!-- #comment-## -->

<li class="comment odd alt thread-even depth-1" id="comment-33425">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">aditi</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2015-10-26T12:16:11+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-33425" class="comment-time-link" itemprop="url">October 26, 2015 at 12:16 pm</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>Hi…it is really easy explanation .thanks for that…do you have any good material in angularjs?</p>
	</div>

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-33425" onclick="return addComment.moveForm( &quot;comment-33425&quot;, &quot;33425&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to aditi">Reply</a></div>
	
</article>
</li><!-- #comment-## -->

<li class="comment even thread-odd thread-alt depth-1" id="comment-33328">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">Russ Ray</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2015-10-08T15:10:20+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-33328" class="comment-time-link" itemprop="url">October 8, 2015 at 3:10 pm</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>First of all, THANK YOU!  A well written article and you explained the concepts so clearly.  The code examples for marvelous!   When is your book coming out? 😉</p>

Russ

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-33328" onclick="return addComment.moveForm( &quot;comment-33328&quot;, &quot;33328&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Russ Ray">Reply</a></div>
	
</article>
<ul class="children">

<li class="comment byuser comment-author-pankaj bypostauthor odd alt depth-2" id="comment-33338">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name"><a href="https://www.journaldev.com" class="comment-author-link" rel="external nofollow" itemprop="url">Pankaj</a></span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2015-10-09T05:38:58+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-33338" class="comment-time-link" itemprop="url">October 9, 2015 at 5:38 am</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>Thanks for the appreciation Russ, there are few eBooks I have written that you can download after subscribing to newsletter.</p>
	</div>

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-33338" onclick="return addComment.moveForm( &quot;comment-33338&quot;, &quot;33338&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Pankaj">Reply</a></div>
	
</article>
</li><!-- #comment-## -->
<li class="comment even thread-even depth-1" id="comment-32817">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">laudukang</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2015-07-21T21:56:30+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-32817" class="comment-time-link" itemprop="url">July 21, 2015 at 9:56 pm</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>thanks a lot.</p>
	</div>

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-32817" onclick="return addComment.moveForm( &quot;comment-32817&quot;, &quot;32817&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to laudukang">Reply</a></div>
	
</article>
</li><!-- #comment-## -->

<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-32557">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">ramesh</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2015-05-30T18:57:01+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-32557" class="comment-time-link" itemprop="url">May 30, 2015 at 6:57 pm</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>Hi Pankaj,</p>

In my project we have one batch running more time.
The scenario is one record getting from one database and inserting into another database.
It is happening record by record. I want to execute these parallarly.

In this case can I implement collable?

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-32557" onclick="return addComment.moveForm( &quot;comment-32557&quot;, &quot;32557&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to ramesh">Reply</a></div>
	
</article>
<ul class="children">

<li class="comment even depth-2" id="comment-37847">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">Vishal</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2017-04-04T03:26:24+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-37847" class="comment-time-link" itemprop="url">April 4, 2017 at 3:26 am</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>Yes You can do the same by adding an extra flag. Set the flag value when any of your thread pick the row to insert.</p>
	</div>

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-37847" onclick="return addComment.moveForm( &quot;comment-37847&quot;, &quot;37847&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Vishal">Reply</a></div>
	
</article>
</li><!-- #comment-## -->
<li class="comment odd alt thread-even depth-1" id="comment-32534">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">Binh Thanh Nguyen</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2015-05-27T03:33:38+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-32534" class="comment-time-link" itemprop="url">May 27, 2015 at 3:33 am</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>Thanks, nice post</p>
	</div>

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-32534" onclick="return addComment.moveForm( &quot;comment-32534&quot;, &quot;32534&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Binh Thanh Nguyen">Reply</a></div>
	
</article>
</li><!-- #comment-## -->

<li class="comment even thread-odd thread-alt depth-1" id="comment-30550">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">Hemanth</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2014-10-21T03:34:38+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-30550" class="comment-time-link" itemprop="url">October 21, 2014 at 3:34 am</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>Nice examples you have given pankaj.</p>

Great Job 🙂

Thanks.

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-30550" onclick="return addComment.moveForm( &quot;comment-30550&quot;, &quot;30550&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Hemanth">Reply</a></div>
	
</article>
</li><!-- #comment-## -->

<li class="comment odd alt thread-even depth-1" id="comment-30155">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">Praveen</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2014-09-15T09:47:40+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-30155" class="comment-time-link" itemprop="url">September 15, 2014 at 9:47 am</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>You are the best and my favorite in Threading concepts!!</p>
	</div>

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-30155" onclick="return addComment.moveForm( &quot;comment-30155&quot;, &quot;30155&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Praveen">Reply</a></div>
	
</article>
</li><!-- #comment-## -->

<li class="comment even thread-odd thread-alt depth-1" id="comment-29079">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">Sayantan</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2014-06-03T02:19:11+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-29079" class="comment-time-link" itemprop="url">June 3, 2014 at 2:19 am</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>Hi Pankaj. One doubt regarding Callable interface. The compiler creates a synthetic method to invoke the call() method. Why is it required?</p>
	</div>

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-29079" onclick="return addComment.moveForm( &quot;comment-29079&quot;, &quot;29079&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Sayantan">Reply</a></div>
	
</article>
<ul class="children">

<li class="comment byuser comment-author-pankaj bypostauthor odd alt depth-2" id="comment-29087">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name"><a href="https://www.journaldev.com" class="comment-author-link" rel="external nofollow" itemprop="url">Pankaj</a></span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2014-06-03T09:36:54+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-29087" class="comment-time-link" itemprop="url">June 3, 2014 at 9:36 am</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>I didn’t understood what you mean here?</p>
	</div>

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-29087" onclick="return addComment.moveForm( &quot;comment-29087&quot;, &quot;29087&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Pankaj">Reply</a></div>
	
</article>
</li><!-- #comment-## -->
<li class="comment even thread-even depth-1" id="comment-28503">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">HIMANSU NAYAK</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2014-04-09T03:53:30+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-28503" class="comment-time-link" itemprop="url">April 9, 2014 at 3:53 am</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>Hi Pankaj,<br>

We normally pass a “User/Worker Thread instance” to the ThreadPoolExecutor/Scheduler but here the Callable Interface is not extending Runnable interface neither we are overriding any run() method. We are creating 100 callable instance but not thread, does it mean we cannot use callable for multi-threading purpose


	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-28503" onclick="return addComment.moveForm( &quot;comment-28503&quot;, &quot;28503&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to HIMANSU NAYAK">Reply</a></div>
	
</article>
<ul class="children">

<li class="comment byuser comment-author-pankaj bypostauthor odd alt depth-2" id="comment-29088">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name"><a href="https://www.journaldev.com" class="comment-author-link" rel="external nofollow" itemprop="url">Pankaj</a></span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2014-06-03T09:39:37+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-29088" class="comment-time-link" itemprop="url">June 3, 2014 at 9:39 am</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>We are creating only 1 instance of Callable implementation. When we submit it to the Executor, it creates the thread for us and execute it. This is multithreading.</p>
	</div>

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-29088" onclick="return addComment.moveForm( &quot;comment-29088&quot;, &quot;29088&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Pankaj">Reply</a></div>
	
</article>
</li><!-- #comment-## -->

<li class="comment even depth-2" id="comment-32244">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">X</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2015-04-18T12:48:49+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-32244" class="comment-time-link" itemprop="url">April 18, 2015 at 12:48 pm</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>e.execute(r);</p>

is the same that…

(new Thread(new Runnable() {…})).start();

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-32244" onclick="return addComment.moveForm( &quot;comment-32244&quot;, &quot;32244&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to X">Reply</a></div>
	
</article>
</li><!-- #comment-## -->
<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-23179">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">Agha Khan</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2013-09-06T23:33:43+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-23179" class="comment-time-link" itemprop="url">September 6, 2013 at 11:33 pm</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>All the tutorial are very Good and helpful. I want to run more than 1000 threads (Tasks) but not at a time may be 20 at  a time using one of thread pools<br>

But I also want to cancel the task if it is blocked for more than 30 minutes. But that 30 minutes are not including submit times of the task. No more than 30 minutes should be used for execution.

Is there any function for any class using that can put the maximum execution time? If yes will that function interrupt the thread that is executing the task? If yes then do we need to handle “ InterruptedException” for that thread?

Please give your best suggestion and thanks for this good articles

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-23179" onclick="return addComment.moveForm( &quot;comment-23179&quot;, &quot;23179&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Agha Khan">Reply</a></div>
	
</article>
</li><!-- #comment-## -->

<li class="comment even thread-even depth-1" id="comment-22907">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">Silviu Burcea</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2013-08-30T06:23:04+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-22907" class="comment-time-link" itemprop="url">August 30, 2013 at 6:23 am</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>Hi,</p>

I believe that your implementation is not the best you can do in this scenario. Your callable waits for exactly 1 second, but if there was a little bit of random, you will probably notice that the get() method, a blocking one, will slow down the execution in the for loop(because some of the results are not ready yet). Rewrite it using a while(!list.isEmpty()) block, every time you have a Future ready(use isDone()) you can safely get the result without delaying the execution and remove it from the list.

HTH

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-22907" onclick="return addComment.moveForm( &quot;comment-22907&quot;, &quot;22907&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Silviu Burcea">Reply</a></div>
	
</article>
<ul class="children">

<li class="comment byuser comment-author-pankaj bypostauthor odd alt depth-2" id="comment-22932">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name"><a href="https://www.journaldev.com" class="comment-author-link" rel="external nofollow" itemprop="url">Pankaj</a></span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2013-08-30T20:57:30+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-22932" class="comment-time-link" itemprop="url">August 30, 2013 at 8:57 pm</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>Hi Silviu,</p>

This is just an example to show how can we use it, in real life scenarios most of the times we will start a callable and then we get the response later on. Obviously we can modify for better performance but if I will add too much implementation, we will loose focus on learning the Callable-Future usage.

Thanks,
Pankaj

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-22932" onclick="return addComment.moveForm( &quot;comment-22932&quot;, &quot;22932&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Pankaj">Reply</a></div>
	
</article>
</li><!-- #comment-## -->

<li class="comment even depth-2" id="comment-32245">
<article itemprop="comment" itemscope="" itemtype="https://schema.org/Comment">

	
	<header class="comment-header">
		<p class="comment-author" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
			<span itemprop="name">Rvindranath</span> <span class="says">says</span>			</p>

		<p class="comment-meta"><time class="comment-time" datetime="2015-04-19T01:44:14+00:00" itemprop="datePublished"><a href="https://www.journaldev.com/1090/java-callable-future-example#comment-32245" class="comment-time-link" itemprop="url">April 19, 2015 at 1:44 am</a></time></p>		</header>

	<div class="comment-content" itemprop="text">
		
		<p>If we use while(!list.isEmpty()) { future.iosDone() … }, don’t we consume waste CPU cycles checking for isDone continuously ?</p>
	</div>

	<div class="comment-reply"><a rel="nofollow" class="comment-reply-link" href="#comment-32245" onclick="return addComment.moveForm( &quot;comment-32245&quot;, &quot;32245&quot;, &quot;respond&quot;, &quot;1090&quot; )" aria-label="Reply to Rvindranath">Reply</a></div>
	
</article>
</li><!-- #comment-## -->

Leave a Reply

Your email address will not be published. Required fields are marked *

posted @ 2018-07-26 17:45  星朝  阅读(415)  评论(0编辑  收藏  举报