How to track asynchronous requests in Java

I have 2 classes. One (A) collects some data, and the other (B) sends data to TCP / IP clients. The process is asynchronous with an update rate of almost zero to several seconds. Please note that this application does not have a graphical interface, so I will not be able to use many of the built-in listeners in "onChange".

Under normal circumstances, I’ll just write the code so that A calls the “send” method to B, passing the data, no problem here.

Now suppose that the data collection rate of A is critical (in 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

  • A puts data in a field in B
  • B has a continuous loop that checks if the data is new or now. If new, he will send it.

If the data is updated several times during sending, it does not matter if it does not slow down A. Creating a new thread for each send would not slow down A in principle, but it will probably lead to a mess.

You can see that B runs in synchronous mode (but A does not), and it is implemented with a while loop with a call to Thread.sleep (). My questions:

  • Should I use a timer task instead of a while loop? I know that most people hate calling Thread.sleep (), but ultimately the only thing I'm interested in is keeping the processor low.

  • , ? A 1 , , , . 25 . , .

* : , A , B . , B *

? , .

!

+5
4

:

A " " . , / .

B , , (-). , , B , .

Object lock = new Object();
Object pending = null;

public void post(Object message) {
    synchronized (lock) {
        pending = message;
        lock.notifyAll();
    }
}

public Object getNextMessage() {
    Object message;
    synchronized (lock) {
        while (pending == null) {
            try {
                lock.wait();
            } catch (InterruptedException e) {
                // Ignore
            }
        }
        message = pending;
        pending = null;
    }
    return message;
}

,

BlockingDeque<Object> queue = new LinkedBlockingDeque<Object>(1);

public void postMessage(Object message) {
    // If previous message is still pending we replace it.
    queue.clear();
    queue.offer(message);
}

public Object getNextMessage() {
    while (true) {
        try {
            return queue.take();
        } catch (InterruptedException e) {
            // Ignore interrupts
        }
    }
}

, , () , .

+2

LinkedBlockingQueue A B, A, . A , , . B, , .

, B A , Observer.

  • , A , - Observable.
  • B .
  • , A , , B - .
  • B .
  • B , . TimerTask.
  • B , A .
+2

Exchanger

B , ( A )
, , .

A 0, , B , , , A , B .

, , B , ( - 0 , B , , )

+2

The most elegant way is a message queue . Awrites data to the queue as soon as it is available. B subscribes to the queue and is notified of each new entry. The message queue handles everything for you.

However, you should be more explicit: should I Breceive notifications for each message? What happens if the update is lost?

+1
source

All Articles