• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion about callable vs runnable  RSS feed

 
Raymond Holguin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So my understanding is that Callable is just Runnable with some extra goodies, mainly the ability to return a result when completed. Now I have read that when you reference the callable.get() method, the execution of the calling thread will wait until the result is available. The issue is that even though I don't use the get() method, my program still seems to be waiting for completion before continuing on.



When i run this code, c2.call() does not execute until c1.call() is completed. I don't understand why this is happening, it should not be waiting for anything. Seems to defeat the purpose of a concurrent process when they actually run sequentially.

Using the same example but with Runnable's, it executes as expected



I get overlapping output messages and everything is great...but not when using Callable. any ideas?
 
Raymond Holguin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nevermind I'm an idiot. Copy and pasting error put my c1 and c2 at the same sleep delay so that was causing issues.
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i run this code, c2.call() does not execute until c1.call() is completed.

I get the impression that you expect each of these call()s to spawn new threads. But that's not how the API is used.
In your current code, there is only *1* thread ever. In that thread, c1.call() puts that single thread to sleep for 5 seconds.
Only after 5 seconds have passed, the CPU starts executing anything further on this thread, which happens to be c2.call().
That's why c2.call() starts only after c1.call() is completed.

The correct way to spawn threads with Callable is using the Executors / ExecutorService class (see tutorial here)
 
Raymond Holguin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthik Shiraly wrote:
When i run this code, c2.call() does not execute until c1.call() is completed.

I get the impression that you expect each of these call()s to spawn new threads. But that's not how the API is used.
In your current code, there is only *1* thread ever. In that thread, c1.call() puts that single thread to sleep for 5 seconds.
Only after 5 seconds have passed, the CPU starts executing anything further on this thread, which happens to be c2.call().
That's why c2.call() starts only after c1.call() is completed.

The correct way to spawn threads with Callable is using the Executors / ExecutorService class (see tutorial here)


Thanks, while you are correct that Executors are the way to go after doing some research on them...i believe you may incorrect about the single thread part. The only reason they were firing off sequentially is because I mistakenly had c1 sleep for 5 seconds. When i changed it to 1 second like I originally though I had it it worked identical to how my second example worked where I used Thread objects instead of Callable, which is what i was expecting. I think the Executors are just threadpool managers that make it easier to maintain the threads your running, but not a requirement to use Callables as a thread.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please post complete code on what you are trying to do.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!