java.util.concurrent.Callable way of creating threads

Hi,

To create thread i am only familiar with 2 ways.
1. extending Thread class
2. implementing Runnable interface.

I am not familiar with below third method
java.util.concurrent.Callable way of creating threads

When this 3rd way is introduced and what are advantaged and disadvantages of using it over other two ways. please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CPColinSenior Java ArchitectCommented:
According to the Javadocs, the Callable interface was introduced in Java 1.5. See ExecutorService.submit(Callable) for more information on how you'd use it. In Java 8, you could even do something like this:

ExecutorService executorService = ExecutorService.newCachedThreadPool();
Future<SomeClass> future = executorService.submit(SomeClass::computeValue);

Open in new window


This code will execute that computeValue() method on a separate thread and return the result in that Future object.

As for which style you should use, it really just depends on what you're doing and how you want to do it. If you're doing something that's really simple, you might as well implement Runnable and skip the extra complexity of using ExecutorService.
dpearsonCommented:
The main reason to use Callable is when you want to get something back from the thread.

In other words the difference between it and Runnable is that Runnable doesn't return a value - it's method (run) return void.  Callable's method (call) returns an Object.

Also Callable's call method is allowed to thrown an exception, so you can also retrieve exceptions from threads, which otherwise is not so easy.

Aside from that it's just like using Runnable.

And FYI, these days you should never be extending Thread.  That's very much old school and the Executor framework makes it totally unnecessary.

Doug

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.