Question: Question 1 / / Creating and starting threads Thread bookingValidationThread = new BookingValidationThread ( ) ; ; Thread paymentProcessingThread = new PaymentProcessingThread ( ) Thread
Question Creating and starting threads
Thread bookingValidationThread new BookingValidationThread
;
;
Thread paymentProcessingThread new PaymentProcessingThread
Thread confirmationThread new ConfirmationThread;
bookingValidationThread.setPriority ThreadMAXPRIORITY;
paymentProcessingThread.setPriorityThreadNORMPRIORITY;
confirmationThread.setPriorityThreadMINPRIORITY;
bookingValidationThread.start;
paymentProcessingThread.start;
confirmationThread.start;
static class BookingValidationThread extends Thread
@Override
public void run
synchronized SwiftBookingSystemclass
System.out.printInBooking validation started.";
try
Thread.sleep; Simulate time taken for
validation
catch InterruptedException e
eprintStackTrace;
isBookingValidated true;
System.out.printInBooking validation completed.";
SwiftBookingSystem.class.notifyAll; Notify
waiting threads
Implement the PaymentProcessingThread class here
static class PaymentProcessingThread extends Thread
@Override public void run
Your implementation goes here
static class ConfirmationThread extends Thread
@Override
public void run
synchronized SwiftBookingSystemclass
try
while isPaymentProcessed
System.out.printInWaiting for payment
processing... ;
SwiftBookingSystem.class.wait; Wait for
payment to be processed
System.out.printInSending confirmation...";
Thread.sleep; Simulate time taken for
confirmation
System.out.printlnBooking confirmed successfully
;
catch InterruptedException e
eprintStackTrace;
Instructions:
Implement the PaymentProcessingThread class so that it:
Waits for the booking validation to complete before starting pay
ment processing.
Processes the payment simulate with Thread.sleep
Sets the isPaymentProcessed flag to true after processing.
Notifies any threads waiting for payment to be processed. Use appropriate synchronization to ensure thread safety and avoid
deadlocks.
Make sure that the ConfirmationThread proceeds only after the pay
ment has been processed.
Notes:
You should only modify the PaymentProcessingThread class.
Pay attention to synchronization blocks synchronized and the use
of wait and notifyAll
Test your implementation to ensure that the output follows the cor
rect sequence:
Booking validation starts and completes.
Payment processing starts and completes.
Confirmation is sent.
Example Output:
Booking validation started.
Booking validation completed.
Payment processing started.
Payment processing completed.
Sending confirmation...
Booking confirmed successfully.
Submission:
Submit the complete code for the PaymentProcessingThread class.
Ensure your code is wellformatted and includes any necessary error
handling.
Answer the following questions
Thread Prioritization
Based on the provided code, identify which thread has the highest
priority.
Explain how thread prioritization affects the execution order of tasks
in the system.
The fictional company SwiftBookings specializes in managing online reser
vations for various events. The company uses a multithreaded system to
handle the booking pipeline efficiently, ensuring that tasks like booking vali
dation, payment processing, and confirmation occur concurrently to improve
user experience.
The SwiftBookings System is divided into three key operations, each in
tended to be handled by a different thread:
BookingValidationThread: This thread checks if there are available
slots for a reservation and validates the details provided by the user.
PaymentProcessingThread: After the booking is validated, this thread
processes the payment for the booking. If the payment is successful, it
notifies the system that the booking can proceed to confirmation.
ConfirmationThread: Once the payment is processed, this thread
sends the booking confirmation to the customer and updates the reser
vation system.
Due to recent issues with bookings getting stuck in the system, the develop
ment team wants to refactor the thread management to improve synchro
nization and avoid deadlocks.
Implement the PaymentProcessingThread class in the code below.
Ensure that your implementation correctly synchronizes with the other
threads, waits for booking validation to complete before processing
payment, and notifies the confirmation thread upon completion.
Here is the existing code:
class SwiftBookingSystem
Shared resources
private static boolean isBookingValidated false;
private static boolean isPaymentProcessed false;
public static void mainString args
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
