Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Here the problem is with the <code>synchronized (o)</code> function.</strong> </p> <blockquote> <p>The synchronized function makes the thread synchronized and there by it only execute only one thread at a time to the object <code>o</code>. Hence while the value of <code>writing</code> is true. It wont allow the 2nd tread <code>readerThread</code> to execute the due to the <code>synchronized (o)</code> in the <code>readerThread</code></p> <p><strong>You are getting an Infinite loop because there is no terminate statement.</strong><br> Look <a href="https://stackoverflow.com/questions/1269931/when-does-a-java-thread-reach-the-die-state"><strong><em>here</em></strong></a> to know when thread terminates</p> </blockquote> <p><strong>Look at this code to Know more about synchronized function</strong> </p> <pre><code>synchronized(object) { // statements to be synchronized } </code></pre> <blockquote> <p>Here, object is a reference to the object being synchronized. A synchronized block ensures that a call to a method that is a member of object occurs only after the current thread has successfully entered object's monitor</p> </blockquote> <p>.</p> <p><strong>Read this to check about the notify methods</strong></p> <p>The Object class in JavaSW has three final methods that allow threads to communicate about the locked status of a resource. These methods are <strong>wait(), notify(), and notifyAll()</strong>. A thread obtains a lock for a particular resource via a synchronized block with an instance of that resource. Suppose that a thread requires that another thread perform a certain action on the resource before it acts on the resource. That thread can synchronize on the resource and call the wait() method on resource. <strong>This says that the thread will wait until it has been notified that it can proceed to act.</strong> </p> <blockquote> <p>The wait() method can take an optional timeout value as a parameter. If this value is used, it means that the thread will either wait until it's notified or it will continue to execute once the timeout value has passed.</p> </blockquote> <p>If a thread is required to perform a task on a resource before another thread operates on the resource (and the other thread is waiting via the <code>wait()</code> method on the resource), the thread needs synchronize on the resource. It can perform its actions on the resource. </p> <p>In order to notify the waiting thread once these actions have completed, the <code>notify()</code> method on the resource is called. This notifies the waiting thread that it can proceed to act. If multiple threads are waiting for the resource, there is no guarantee as to which thread will be given access to the resource. If it is desired for all waiting threads to be awoken, the <code>notifyAll()</code> method can be called on the resource. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload