{"id":22513446,"url":"https://github.com/emahtab/scheduled-thread-pool","last_synced_at":"2025-03-28T01:23:47.887Z","repository":{"id":260618776,"uuid":"881852339","full_name":"eMahtab/scheduled-thread-pool","owner":"eMahtab","description":"Single thread Scheduled Thread Pool to run a task periodically","archived":false,"fork":false,"pushed_at":"2024-11-01T11:38:27.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-02T03:19:16.795Z","etag":null,"topics":["concurrency","java","scheduled-thread-pool","thread-pool"],"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-01T11:31:52.000Z","updated_at":"2024-11-01T11:38:44.000Z","dependencies_parsed_at":"2024-11-01T12:25:34.820Z","dependency_job_id":"3cba071c-5482-46c8-afa1-2611d1f016fe","html_url":"https://github.com/eMahtab/scheduled-thread-pool","commit_stats":null,"previous_names":["emahtab/scheduled-thread-pool"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fscheduled-thread-pool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fscheduled-thread-pool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fscheduled-thread-pool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fscheduled-thread-pool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/scheduled-thread-pool/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245950639,"owners_count":20699103,"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":["concurrency","java","scheduled-thread-pool","thread-pool"],"created_at":"2024-12-07T03:12:22.950Z","updated_at":"2025-03-28T01:23:47.870Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Scheduled Thread Pool\n\nBelow example uses ScheduledExecutorService with a scheduled thread pool. We create a scheduler that performs a regular task—such as checking for system status updates—every 10 seconds.\n\nThis approach is commonly used in applications to execute periodic tasks, like monitoring servers, performing database cleanups, or sending notifications.\n\n```java\nimport java.util.concurrent.Executors;\nimport java.util.concurrent.ScheduledExecutorService;\nimport java.util.concurrent.TimeUnit;\n\nclass SystemStatusChecker {\n\n    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);\n    private static final int INITIAL_DELAY = 5;\n    private static final int CHECK_INTERVAL = 10;\n    private static final int SHUTDOWN_TIMEOUT = 5;\n\n    public void startSystemCheck() {\n        scheduler.scheduleAtFixedRate(this::checkSystemStatus, INITIAL_DELAY, CHECK_INTERVAL, TimeUnit.SECONDS);\n        System.out.println(\"System status checker started.\");\n    }\n\n    private void checkSystemStatus() {\n        System.out.println(\"Checking system status at \" + System.currentTimeMillis());\n\n        // Simulate system health check logic\n        double healthCheckOutcome = Math.random();\n        if (healthCheckOutcome \u003c 0.5) {\n            System.out.println(\"ALERT: System health warning!\");\n        } else {\n            System.out.println(\"System status: OK\");\n        }\n    }\n\n    public void stopSystemCheck() {\n        System.out.println(\"Stopping system status checker...\");\n        scheduler.shutdown();\n        try {\n            if (!scheduler.awaitTermination(SHUTDOWN_TIMEOUT, TimeUnit.SECONDS)) {\n                System.out.println(\"Forcing shutdown due to delay.\");\n                scheduler.shutdownNow();\n            } else {\n                System.out.println(\"System status checker stopped successfully.\");\n            }\n        } catch (InterruptedException e) {\n            System.err.println(\"Scheduler interrupted during shutdown.\");\n            scheduler.shutdownNow();\n            Thread.currentThread().interrupt();\n        }\n    }\n}\n\npublic class Main {\n    public static void main(String[] args) {\n        SystemStatusChecker checker = new SystemStatusChecker();\n        checker.startSystemCheck();\n\n        // Simulate running the checker for 1 minute\n        try {\n            Thread.sleep(60000);\n        } catch (InterruptedException e) {\n            System.err.println(\"Main thread interrupted.\");\n            Thread.currentThread().interrupt();\n        }\n\n        checker.stopSystemCheck();\n    }\n}\n\n```\n\n### Execution Output\n```\nSystem status checker started.\nChecking system status at 1730460894421\nSystem status: OK\nChecking system status at 1730460904418\nALERT: System health warning!\nChecking system status at 1730460914419\nSystem status: OK\nChecking system status at 1730460924418\nSystem status: OK\nChecking system status at 1730460934418\nALERT: System health warning!\nChecking system status at 1730460944418\nSystem status: OK\nStopping system status checker...\nSystem status checker stopped successfully.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fscheduled-thread-pool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fscheduled-thread-pool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fscheduled-thread-pool/lists"}