Question: Question 1 / / Creating and starting threads Thread bookingValidationThread = new BookingValidationThread ( ) ; ; Thread paymentProcessingThread = new PaymentProcessingThread ( ) Thread

Question 1// Creating and starting threads
Thread bookingValidationThread = new BookingValidationThread()
;
;
Thread paymentProcessingThread = new PaymentProcessingThread()
Thread confirmationThread = new ConfirmationThread();
bookingValidationThread.setPriority (Thread.MAX_PRIORITY);
paymentProcessingThread.setPriority(Thread.NORM_PRIORITY);
confirmationThread.setPriority(Thread.MIN_PRIORITY);
bookingValidationThread.start();
paymentProcessingThread.start();
confirmationThread.start();
}
static class BookingValidationThread extends Thread {
@Override
public void run(){
synchronized (SwiftBookingSystem.class){
System.out.printIn("Booking validation started.");
try {
Thread.sleep(1000); // Simulate time taken for
validation
} catch (InterruptedException e){
e.printStackTrace();
}
isBookingValidated = true;
System.out.printIn("Booking 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 (SwiftBookingSystem.class){
try {
while (!isPaymentProcessed){
System.out.printIn("Waiting for payment
processing... ');
SwiftBookingSystem.class.wait(); // Wait for
payment to be processed
}
System.out.printIn("Sending confirmation...");
Thread.sleep(1000); // Simulate time taken for
confirmation
System.out.println("Booking confirmed successfully
.");
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
}
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 well-formatted and includes any necessary error
handling.
(1.2) 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.
(1.1) 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 main(String[] args){
Question 1 / / Creating and starting threads

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!