Releasing locks:
Java provides a solution to this problem in a method which allows a thread to go into the blocked state and to release the hold that it has on a lock. When a thread is sent into the blocked state using the sleep method, it retains the lock. To get over this problem the wait method puts the thread into the blocked state and releases the lock that it holds. Threads put into the blocked state can be made runnable using the methods notify and notifyAll. All of these methods are part of the Object class, not the Thread class, because every object has a lock even though it is used only by a programmer when dealing with threads.
When a thread accesses an object - for example, to read data - it may have to invoke the wait method if it finds that there is no data to be read. Having invoked the wait method, the thread then becomes blocked, is placed in a set of threads that are waiting to access the object and releases the lock that it held. This gives another thread the chance to add an item to the buffer. When the reading thread is activated again, there is now data for it to read.
The wait method is associated with two methods known as notify and notifyAll. One of these methods should be called when the state of an object has changed and the threads in the set for that object can be given a chance to access it.
Invoking a notify method informs the scheduler that another thread can now be moved to the runnable state; if it is then selected to become running, it can try to access the object. An important point to note is that the thread selected when the notify method is invoked is an arbitrary choice by the scheduler, and will not necessarily be the first thread that began waiting. Invoking the notifyAll method will move all the threads currently in the blocked state into the runnable state.
The code for the two methods now becomes:
public synchronized void addItemToBuffer (int i)
{
while (numberInBuffer == bufferLength)
{
try
{
}
wait();
catch (InterruptedException e)
{
String errMessage = e.getMessage();
System.out.println("Error " + errMessage);
}
}
if (end == bufferLength - 1)
{
end = 0;
}
else
{
end++;
}
buffer[end] = i; numberInBuffer++;
notifyAll();
}
public synchronized int removeItemFromBuffer ()
{
int value;
while (numberInBuffer == 0)
{
try
{
}
wait();
catch (InterruptedException e)
{
String errMessage = e.getMessage();
System.out.println("Error " + errMessage);
}
}
value = buffer[front];
if (front == bufferLength - 1)
{
front = 0;
}
else
{
front++;
}
numberInBuffer- -; notifyAll();
return value;
}
In both methods, the method notifyAll is called after all the work is done, in order to give another thread the opportunity to execute.
This approach translates into a simple set of rules as follows.
- If two or more threads modify a common object, then declare the methods that do the modifying as being synchronized.
- If a thread needs to wait for an object to change, make sure that it waits inside the thread. It does this by entering a synchronized method and calling wait. In fact, wait can be used only inside a synchronized method.
- When a method has changed an object, it should call the notify method or the notifyAll method before terminating. This gives other methods a chance to access the object. Like wait, the notify and notifyAll methods can be used only inside a synchronized method.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Releasing locks homework and assignments? Live Releasing locks experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Releasing locks homework help, java assignment help and Releasing locks projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours