Java - networking - Best Practice - mixed synchronous / asynchronous commands?

In principle, you need a registry of blocked threads (or better, the locks on which they are waiting), keyed with some identifier which will be sent by the remote side For asynchronous operation, you simply sent the message and proceed For synchronous operation, after sending the message, your sending thread (or the thread which initiated this) create a lock object, adds this with some key to the registry and then waits on the lock until notified The reading thread, when it receives some answer, looks in the registry for the lock object, adds the answer to it, and calls notify() Then it goes reading the next input The hard work here is the proper synchronization to avoid dead locks as well as missing a notification (because it comes back before we added ourself to the registry) I did something like this when I implemented the remote method calling protocol for our Fencing applet. In principle RMI works the same way, just without the asynchronous messages.

In principle, you need a registry of blocked threads (or better, the locks on which they are waiting), keyed with some identifier which will be sent by the remote side. For asynchronous operation, you simply sent the message and proceed. For synchronous operation, after sending the message, your sending thread (or the thread which initiated this) create a lock object, adds this with some key to the registry and then waits on the lock until notified.

The reading thread, when it receives some answer, looks in the registry for the lock object, adds the answer to it, and calls notify(). Then it goes reading the next input. The hard work here is the proper synchronization to avoid dead locks as well as missing a notification (because it comes back before we added ourself to the registry).

I did something like this when I implemented the remote method calling protocol for our Fencing-applet. In principle RMI works the same way, just without the asynchronous messages.

So I would do something like: reader. WaitForCmd("ABC_ACK") which internally adds something to a list, waits and is notified (resumed) when the ABC_ACK arrived via the reading-thread? – kazu Jun 5 at 8:29 Yes, someone like this.

I would use a map instead of a list (to allow efficient retrieval of the right lock even if lots of threads are waiting), though. – PaÅ­lo Ebermann Jun 5 at 11:36 works like a charm, thanks :) – kazu Jun 5 at 12:35.

Paulo's solution is one I have used before. However, there may be a simpler solution. Say you don't have a background thread reading results in the connection.

What you can do instead do is use the current thread to read any results. // Asynchronous call conn. SendMessage("Async-request"); // server sends no reply.

// Synchronous call. Conn. SendMessage("Sync-request"); String reply = conn.readMessage().

Yeah, I used your approach before, but this only works if I know what packet will appear next. If there is a background-data-flow, the next packet could be a data packet and not the ACK i'm waiting for. That's my problem.

The background thread mainly handles the data-packets as this is easy and does not need to be synchronized. – kazu Jun 5 at 8:19 I would use a separate connection for background data flow, if you have any. The other option is to have readMessage wait until it read a matching requestId.

You could have the send messages also check for data to be read. This is only useful if to are periodically check the stream and not using blocking IO. – Peter Lawrey Jun 5 at 9:34.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions