{"id":5039,"date":"2024-03-31T11:18:35","date_gmt":"2024-03-31T11:18:35","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=5039"},"modified":"2024-03-31T12:30:59","modified_gmt":"2024-03-31T12:30:59","slug":"simple-ticket-reservation-system","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/simple-ticket-reservation-system\/","title":{"rendered":"Simple Ticket Reservation System"},"content":{"rendered":"<ol>\n<li>We use ReentrantLock for locking the Resource(totalSeats)<\/li>\n<li>Incase anything goes wrong (Exception being thrown etc.) you want to make sure the lock is released no matter what.<\/li>\n<li>Calling the reserveSeats method should be done inside separate threads<\/li>\n<\/ol>\n<p><strong>ReservationSystem.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.concurrent.locks.Lock;\r\nimport java.util.concurrent.locks.ReentrantLock;\r\n\r\npublic class ReservationSystem {\r\n    private Integer totalSeats;\r\n    private final Lock lock = new ReentrantLock();\r\n\r\n    public ReservationSystem(Integer totalSeats){\r\n        this.totalSeats = totalSeats;\r\n    }\r\n\r\n    public Integer getTotalSeats(){\r\n        return totalSeats;\r\n    }\r\n\r\n    public void reserveSeats(String userName, int numOfSeats){\r\n        lock.lock();\r\n\r\n        try{\r\n            if(numOfSeats &gt;0 &amp;&amp; totalSeats&gt;numOfSeats){\r\n                totalSeats -= numOfSeats;\r\n                System.out.println(userName + &quot; has reserved &quot;+ numOfSeats + &quot; with &quot; + totalSeats + &quot; still available&quot;);\r\n            }else{\r\n                System.out.println(&quot;Seats not Available&quot;);\r\n            }\r\n        }finally {\r\n            lock.unlock();\r\n        }\r\n    }\r\n}\r\n\r\n<\/pre>\n<p><strong>BookSeat.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class BookSeat {\r\n    public static void main(String&#x5B;] args) {\r\n        ReservationSystem objResSys = new ReservationSystem(100);\r\n\r\n        System.out.println(&quot;Total available Seats &quot;+ objResSys.getTotalSeats());\r\n\r\n        Thread objThread1 = new Thread(() -&gt; {objResSys.reserveSeats(&quot;User1&quot;, 10);});\r\n        Thread objThread2 = new Thread(() -&gt; {objResSys.reserveSeats(&quot;User2&quot;, 20);});\r\n        Thread objThread3 = new Thread(() -&gt; {objResSys.reserveSeats(&quot;User3&quot;, 5);});\r\n\r\n\r\n        objThread1.start();\r\n        objThread2.start();\r\n        objThread3.start();\r\n\r\n        try {\r\n            objThread1.join();\r\n            objThread2.join();\r\n            objThread3.join();\r\n        } catch (InterruptedException e) {\r\n            Thread.currentThread().interrupt();\r\n        }\r\n\r\n        System.out.println(&quot;Remaining available Seats &quot;+ objResSys.getTotalSeats());\r\n    }\r\n}\r\n<\/pre>\n<p><strong><\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n<\/pre>\n<pre>\r\nTotal available Seats 100\r\nUser2 has reserved 20 with 80 still available\r\nUser1 has reserved 10 with 70 still available\r\nUser3 has reserved 5 with 65 still available\r\nRemaining available Seats 65\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We use ReentrantLock for locking the Resource(totalSeats) Incase anything goes wrong (Exception being thrown etc.) you want to make sure the lock is released no matter what. Calling the reserveSeats method should be done inside separate threads ReservationSystem.java import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class ReservationSystem { private Integer totalSeats; private final Lock lock = new&hellip; <a href=\"https:\/\/codethataint.com\/blog\/simple-ticket-reservation-system\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[271,141],"tags":[],"class_list":["post-5039","post","type-post","status-publish","format-standard","hentry","category-code-examples-threads","category-threads"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=5039"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5039\/revisions"}],"predecessor-version":[{"id":5040,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5039\/revisions\/5040"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=5039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=5039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=5039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}