?
=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
Hello.
I am working on an assignment for the Parallel Programming course I've
been taking. Among other things, it involves a server which should
accept only 4 client connections. After the fourth connection has been
established, I would like the server to stop listening for connections.
Only when one of the connections is broken should the server resume its
listening activity.
Now the question is how can I properly set things up so that the server
won't be busy-waiting? I thought of an approach using wait/notify but,
since I am still learning concurrent programming, I am afraid I might be
abusing the concepts.
while (true)
{
while (connections < 4)
{
client = server.accept();
// create client handling object
connections++;
}
wait(); // once there are 4 connections already, stand by until
// a notify()cation is issued
}
Does the code above have any chance of succeeding? Will a notify() call
from a different thread actually wake up the server? What does that
wait() call actually means? From what I've read, it means the server
would be giving up its monitor and allowing any other thread who might
be trying to get hold of that monitor to proceed. Looking at it that way
makes me fear my solution will lead to a dead server, since the server
code is not shared, i.e. there are no other threads wanting to get hold
of that monitor.
My post shows I am definitely confused about these concepts. What I am
basically trying to do is have the server wait on a given condition
(number of connections went down from 4) in order to resume working.
What's the proper way to go about it?
Thank you,
I am working on an assignment for the Parallel Programming course I've
been taking. Among other things, it involves a server which should
accept only 4 client connections. After the fourth connection has been
established, I would like the server to stop listening for connections.
Only when one of the connections is broken should the server resume its
listening activity.
Now the question is how can I properly set things up so that the server
won't be busy-waiting? I thought of an approach using wait/notify but,
since I am still learning concurrent programming, I am afraid I might be
abusing the concepts.
while (true)
{
while (connections < 4)
{
client = server.accept();
// create client handling object
connections++;
}
wait(); // once there are 4 connections already, stand by until
// a notify()cation is issued
}
Does the code above have any chance of succeeding? Will a notify() call
from a different thread actually wake up the server? What does that
wait() call actually means? From what I've read, it means the server
would be giving up its monitor and allowing any other thread who might
be trying to get hold of that monitor to proceed. Looking at it that way
makes me fear my solution will lead to a dead server, since the server
code is not shared, i.e. there are no other threads wanting to get hold
of that monitor.
My post shows I am definitely confused about these concepts. What I am
basically trying to do is have the server wait on a given condition
(number of connections went down from 4) in order to resume working.
What's the proper way to go about it?
Thank you,