Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assume a scenario of railway reservation system where two passengers are trying to book a ticket from a certain source to destination in a same

image text in transcribed

Assume a scenario of railway reservation system where two passengers are trying to book a ticket from a certain source to destination in a same train. Both the passengers are trying to book the tickets in the same time. Think that only one berth is available in the train and both the passengers are asking for the same berth. Design a Java program using thread synchronization so that only one ticket is allocated to one person at a time. To design program take a class named as Train_Ticket" that implements runnable. Take two variables such as total_berths_available and berths_requested. Create two threads t1 and t2. When t1 enters the run method, it checks the "total_berths_available" and allot it to the first person. After the allocation of the berth the thread t1 enters try{} block inside the run method and goes to sleep for 5 seconds. During this sleep duration the ticket will be printed in the printer. The program can only compile successfully if you can use a synchronized block effectively. Sample Output: Available berths= 1 1 Berth reserved for First Person Available berths= 0 Sorry! No berths available

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

More Books

Students also viewed these Databases questions