Rubrik | Phone Screen
Anonymous User
2433

Hi, can someone please help me with how to solve this question using Locks ?

Given following classes:

Class Client:
init()
request()
close()

Class ClientWrapper(Client):
init()
request()
close()

Here, class Client is like an abstract class that deals with responsibilities like initializing connection to the server using init(), request() is used to make request to the server and close() is used to close the connection to the server.
Question: Multiple threads can concurrently access methods of class ClientWrapper. One needs to implement all the methods of class ClientWrapper such that following use cases are covered -

  • If init() is in progress by some thread, then block other concurrent threads to execute request() or close() method
  • Do not allow concurrent calls to init() method.
  • Allow concurrent calls to request() method if init() is already called successfully.
  • If some thread is still using request(), then block any other thread to close the connection using close() method
  • Once connection is closed, do not allow subsequent calls to close() method
  • Do not allow concurrent calls to close() method
  • ...etc.

Thanks!!

Comments (6)