Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Multi-Threading in JAVA using Callables

Posted on Sep 14 • Originally published at Medium In multi-threaded programming, we often encounter scenarios where tasks need to be executed concurrently, but they don't necessarily need to return any values. Think of these tasks as workers performing their jobs independently, like chefs work simultaneously to execute their tasks, and they don't need to return any specific information. In such cases, runnables can be used where there is nothing to return.However, there are situations where not only do we need tasks to run concurrently, but we also need them to return some valuable data upon completion. This is where the concept of Callables comes into play.Let’s understand with an example:Imagine you are an online shopper placing multiple orders. You want to track the status of each order and get confirmation when each order is successfully processed. This is where the concept of Callable comes in.To incorporate Callables into multi-threaded programs, you can follow these steps:Identify tasks within your program that require data to be returned upon completion. These tasks are analogous to online shopping orders where you want tracking information.For tasks that need to return data, create classes and implement the Callable interface. This interface allows tasks to return results or throw exceptions, making it suitable for data-retrieval tasks.Inside your Callable class, implement the call() method, which contains the code for the task and defines the return type for the method. This method represents the work that needs to be done and returns the result upon completion.Create a main class that utilizes the ExecutorService to execute the Callable tasks:In the main class, we use Future objects to capture the results of the submitted Callable tasks. A Future represents the result of an asynchronous computation, in this case, the result of each order processing task.In our example scenario of Online Shopping, each order represents a separate task, and we want to keep track of their status and delivery. So:Just like you can check the status of our online orders using the order number without waiting at the store, we can use Future to check the status and retrieve results from Callable tasks without blocking the program.Here's how this works:This allows to efficiently manage tasks that return data in a multi-threaded program. The Future provides a way to obtain the result of the task when it's ready, just like tracking online orders and receiving status updates when the orders are processed.Thank you for reading our blog. We appreciate your time and interest in our content. If you have any questions, feedback, or topics you’d like us to cover in our future articles, please feel free to leave a comment below. Your input is valuable, and it helps us create content that matters to you.Stay connected with us for more insightful articles on various topics related to technology, programming, and much more.Happy coding!Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Matej Bačo - Sep 7 Godot - Sep 9 Muhammad Bin Zafar - Sep 7 Gautam Anand - Sep 3 Once suspended, Codecraftclub will not be able to comment or publish posts until their suspension is removed. Once unsuspended, codecraftclub will be able to comment and publish posts again. Once unpublished, all posts by codecraftclub will become hidden and only accessible to themselves. If codecraftclub is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Code Craft Club. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag codecraftclub: codecraftclub consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging codecraftclub will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Multi-Threading in JAVA using Callables

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×