Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Semaphores 1

Status
Not open for further replies.

tamdrew

Programmer
Mar 19, 2001
2
0
0
GB
Have set up a simple client / server application using RMI, however need to have multiple threads attached to the server that will all return readings to the buffer,
however the server requires exclusive access. In order to implement this I need to use semaphores. Unfortunately I am abit unsure how to do this.

Would anyone like to give me a start on this, or at least a good source of info from a web site.
Thanks
 
You can look at the use of "wait, notify, notifyAll". These work together as semaphores. The following code example is from the Java tutorial. It shows a little of how it's used with a producer/consumer combination.

public synchronized int get() {
while (available == false) {
try {
// wait for Producer to put value
wait();
} catch (InterruptedException e) {
}
}
available = false;
// notify Producer that value has been retrieved
notifyAll();
return contents;
}
public synchronized void put(int value) {
while (available == true) {
try {
// wait for Consumer to get value
wait();
} catch (InterruptedException e) {
}
}
contents = value;
available = true;
// notify Consumer that value has been set
notifyAll();
}


The URL where it comes from is:

Greetings,
Steven.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top