How to monitor Asynchronous requests in Java
<p>I have 2 classes. One (A) collects some data and the other (B) sends the data to TCP/IP clients. The process is asynchronous with refresh rates from nearly zero to a few seconds.
Note that this application has no GUI so I won't be able to use many built in "onChange" listeners.</p>

<p>In normal conditions I would simply write the code so that A calls a "send" method on B, passing the data, no problems here.</p>

<p>Now, assume that the rate A collects data is critical (real time) and that A cannot wait for B to complete the sending process (note that B uses TCP, not UDP). The way I implemented this is</p>

<ul>
<li>A places the data in a field in B</li>
<li>B has a continuous loop that checks if the data is new or now. If new, it will send it out.</li>
</ul>

<p>If during the send the data is updated a few times it doesn't matter, as long as it doesn't slow down A.
Spawning a new thread for each send would in principle not slow down A but it's likely gonna result in a mess.</p>

<p>You can see that B is working in synchronous mode (but A isn't) and it's implemented with a while loop with a Thread.sleep() call. My questions are:</p>

<ol>
<li><p>Should I use a timer task instead of the while loop? I know that most people hate the Thread.sleep() call but ultimately the only thing I'm interested is in keeping CPU low.</p></li>
<li><p>Isn't there a more elegant way than the synchronous approach? In some cases the data refresh of A is about 1 second and it would be nice if I could just have a listener that would act on an event. In such case a sleep time of 25ms would be a waste of cycles. In other cases it's very fast and I'd like no sleep at all.</p></li>
</ol>

<p>*<em>Example: imagine that A is submitting screenshots from your screen and B is sending them to the clients. Only the last one matters and B is gonna go as fast as possible *</em></p>

<p>Any ideas or suggestions? Please keep things as simple and low cpu as possible</p>

<p>thanks a lot!</p>
