I am a beginner in Java language. I have a client server application. In this application, there is a need for mutual exclusion for the resources to be accessed onto the server by multiple clients.
I am trying to achieve mutual exclusion by using a lock variable. But I don't known why the lock variable doesn't get updated when the current thread is done with it's job without a println statement in the loop.
Below is the code snippet for the server.java file -
int shown = 0;
int value = server.lock;
while(value != 1) {
if(shown == 0) {
System.out.println("Server is busy. Please wait");
shown++;
}
value = server.lock;
// Below statement.
System.out.print("\t");
}
shown = 0;
server.lock--;
The lock variable is updated always to 1 whenever the work of the current thread is done
The code works perfectly with a println statement but doesn't work without it. Like the while keeps on repeating. I don't know why.
Any help is appreciated.
Thank you,
P. Parekh