{"id":22513431,"url":"https://github.com/emahtab/callable","last_synced_at":"2025-03-28T01:23:21.988Z","repository":{"id":261187370,"uuid":"883536938","full_name":"eMahtab/callable","owner":"eMahtab","description":"Callable Example","archived":false,"fork":false,"pushed_at":"2024-11-14T15:32:21.000Z","size":13,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-02T03:19:13.592Z","etag":null,"topics":["callable","concurrency","java"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-05T06:19:00.000Z","updated_at":"2024-11-14T15:32:24.000Z","dependencies_parsed_at":"2024-11-05T07:23:28.206Z","dependency_job_id":"04fcfc99-cfe6-4de2-985a-f57fe8873027","html_url":"https://github.com/eMahtab/callable","commit_stats":null,"previous_names":["emahtab/callable"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcallable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcallable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcallable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcallable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/callable/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245950637,"owners_count":20699102,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["callable","concurrency","java"],"created_at":"2024-12-07T03:12:20.632Z","updated_at":"2025-03-28T01:23:21.951Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Callable\n**The Callable interface was introduced in Java 1.5 as part of the java.util.concurrent package.** It was created to address limitations of the Runnable interface.\n\n```java\npublic interface Callable\u003cV\u003e {\n    V call() throws Exception;\n}\n```\n\n#### Callable vs. Runnable\nThe Java Callable interface is similar to the Java Runnable interface, in that both of them represents a task that is intended to be executed concurrently by a separate thread.\n\nA Java Callable is different from a Runnable in that the Runnable interface's run() method does not return a value, and it cannot throw checked exceptions (only RuntimeExceptions).\n\n**Additionally, a Runnable was originally designed for long running concurrent execution, e.g. running a network server concurrently, or watching a directory for new files. The Callable interface is more designed for one-off tasks that return a single result.**\n\n```java\nimport java.util.ArrayList;\nimport java.util.List;\nimport java.util.concurrent.Callable;\nimport java.util.concurrent.ExecutionException;\nimport java.util.concurrent.ExecutorService;\nimport java.util.concurrent.Executors;\nimport java.util.concurrent.Future;\n\n//Callable task that calculates the sum of integers in a given range\nclass SumCalculator implements Callable\u003cInteger\u003e {\n private int start;\n private int end;\n\n public SumCalculator(int start, int end) {\n     this.start = start;\n     this.end = end;\n }\n\n @Override\n public Integer call() {\n     int sum = 0;\n     for (int i = start; i \u003c= end; i++) {\n         sum += i;\n     }\n     System.out.println(Thread.currentThread().getName()+\" Sum from \" + start + \" to \" + end + \" = \" + sum);\n     return sum;\n }\n}\n\npublic class CallableExample {\n    public static void main(String[] args) {\n        // Define the ranges to sum up\n        int[][] ranges = { { 1, 100 }, { 101, 200 }, { 201, 300 }, { 301, 400 }, { 401, 500 } };\n\n        // Create a list to hold Future objects associated with Callable tasks\n        List\u003cFuture\u003cInteger\u003e\u003e futures = new ArrayList\u003c\u003e();\n        // Create a fixed thread pool with 5 threads\n        ExecutorService executorService = Executors.newFixedThreadPool(5);\n        // Submit tasks to executor for each range\n        for (int[] range : ranges) {\n            Callable\u003cInteger\u003e task = new SumCalculator(range[0], range[1]);\n            Future\u003cInteger\u003e future = executorService.submit(task);\n            futures.add(future);\n        }\n        // Collect the results\n        int totalSum = 0;\n        for (Future\u003cInteger\u003e future : futures) {\n            try {\n                totalSum += future.get(); // Future.get() blocks until the result is available\n            } catch (InterruptedException | ExecutionException e) {\n                e.printStackTrace();\n            }\n        }\n        // Shutdown the executor service\n        executorService.shutdown();\n        System.out.println(\"Total Sum: \" + totalSum);\n    }\n}\n```\n\n### Execution Output :\n```\npool-1-thread-5 Sum from 401 to 500 = 45050\npool-1-thread-4 Sum from 301 to 400 = 35050\npool-1-thread-2 Sum from 101 to 200 = 15050\npool-1-thread-3 Sum from 201 to 300 = 25050\npool-1-thread-1 Sum from 1 to 100 = 5050\nTotal Sum: 125250\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fcallable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fcallable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fcallable/lists"}