Posts

Showing posts with the label Java

[Java - Synchronization] Wait() and Notify()

Image
1) Explanation wait() tells the calling thread to wait (possibly forever) until some other thread tells it to resume using the method, notify( ). notify method  is used, with wait(), to wake up threads that are waiting for an access to this object’s monitor. There are two ways to notify; notify() and notifyAll(). Notify() will wake up a  randomly  selected thread waiting on this object’s monitor. Since notify() wakes up a single random thread it can be used to implement mutually exclusive locking where threads are doing similar tasks, but in most cases, it would be more viable to implement notifyAll(). NotifyAll() wakes all threads that are waiting on this object’s monitor. The awakened threads will complete in the usual manner – like any other thread. * There is a tricky part about notify methods, and that is, it only releases the lock after the methods following the notify method is completed as well (usually until it reaches the wait() statemen...

[Java] Servlets

A servlet is a class which responds to a particular type of network request (most commonly an HTTP request). Servlets are usually used to implement web applications. Servlets run in a servlet container, which handles the network side (eg. parsing HTTP request, connection handling.etc). One of the best-known source servlet containers is Tomcat. Flow of Request Client sends HTTP request to Web Server Web Server forwards HTTP request to Web Container Web Container converts request into valid request object (because servlet cannot understand HTTP; only understands objects) Web container spins a thread for each request. All the business logic goes inside doGet() and doPost() methods inside the servlets. Servlet builds a Java response object and sends it to the container. It converts that to HTTP response again and send it to the client.