{"id":22513457,"url":"https://github.com/emahtab/print-in-order","last_synced_at":"2026-04-27T21:31:57.990Z","repository":{"id":260475694,"uuid":"881403392","full_name":"eMahtab/print-in-order","owner":"eMahtab","description":null,"archived":false,"fork":false,"pushed_at":"2024-11-01T16:32:38.000Z","size":8,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T01:28:06.345Z","etag":null,"topics":["concurrency","java","leetcode","semaphore"],"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-10-31T14:02:02.000Z","updated_at":"2024-11-01T16:37:20.000Z","dependencies_parsed_at":"2024-10-31T15:18:25.427Z","dependency_job_id":"118136b0-3628-404f-9cb1-5012c10d252d","html_url":"https://github.com/eMahtab/print-in-order","commit_stats":null,"previous_names":["emahtab/print-in-order"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/print-in-order","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fprint-in-order","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fprint-in-order/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fprint-in-order/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fprint-in-order/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/print-in-order/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fprint-in-order/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32356598,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-27T20:07:02.737Z","status":"ssl_error","status_checked_at":"2026-04-27T20:07:00.910Z","response_time":128,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","leetcode","semaphore"],"created_at":"2024-12-07T03:12:24.821Z","updated_at":"2026-04-27T21:31:57.973Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Print in order\n## https://leetcode.com/problems/print-in-order\n\nSuppose we have a class:\n```java\npublic class Foo {\n  public void first() { print(\"first\"); }\n  public void second() { print(\"second\"); }\n  public void third() { print(\"third\"); }\n}\n```\nThe same instance of Foo will be passed to three different threads. Thread A will call first(), thread B will call second(), and thread C will call third(). Design a mechanism and modify the program to ensure that second() is executed after first(), and third() is executed after second().\n\nNote:\n\nWe do not know how the threads will be scheduled in the operating system, even though the numbers in the input seem to imply the ordering. The input format you see is mainly to ensure our tests' comprehensiveness.\n\n```\nExample 1:\n\nInput: nums = [1,2,3]\nOutput: \"firstsecondthird\"\nExplanation: There are three threads being fired asynchronously. The input [1,2,3] means thread A calls first(),\nthread B calls second(), and thread C calls third(). \"firstsecondthird\" is the correct output.\n\nExample 2:\n\nInput: nums = [1,3,2]\nOutput: \"firstsecondthird\"\nExplanation: The input [1,3,2] means thread A calls first(), thread B calls third(),\nand thread C calls second(). \"firstsecondthird\" is the correct output.\n```\n\n# Implementation : Using Semaphore\n```java\nclass Foo {\n    Semaphore first = new Semaphore(1);\n    Semaphore second = new Semaphore(0);\n    Semaphore third = new Semaphore(0);\n\n    public Foo() {\n        \n    }\n\n    public void first(Runnable printFirst) throws InterruptedException {\n        first.acquire();\n        // printFirst.run() outputs \"first\". Do not change or remove this line.\n        printFirst.run();\n        second.release();\n    }\n\n    public void second(Runnable printSecond) throws InterruptedException {\n        second.acquire();\n        // printSecond.run() outputs \"second\". Do not change or remove this line.\n        printSecond.run();\n        third.release();\n    }\n\n    public void third(Runnable printThird) throws InterruptedException {\n        third.acquire();\n        // printThird.run() outputs \"third\". Do not change or remove this line.\n        printThird.run();\n        third.release();\n    }\n}\n```\n\n# References :\nhttps://jenkov.com/tutorials/java-util-concurrent/semaphore.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fprint-in-order","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fprint-in-order","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fprint-in-order/lists"}