Home java What are callback interfaces and what do they eat with?

What are callback interfaces and what do they eat with?

Author

Date

Category

While studying interfaces, I came across such a thing as a callbak interface. If I understood correctly, then this is a beacon interface that can be screwed to something that will pull this interface in case of any changes (for example, such an interface can be weighed on the communication channel and, in the event of messages arriving, perform some action) and he, in turn, will execute the prescribed methods. The question is how to implement it. Examples from the tutorials that I found on the net are not entirely clear to me. Please explain how this thing is implemented, or share links to good materials on this topic.


Answer 1, authority 100%

callback – callback – the concept and application is very wide.
callbacks like your beacon are called listeners. They are registered by the registration function and then called by someone who is not clear and it is not known when (the event will come or not? When? Who knows …)

There are other types of callbacks.

For example, the Win32 API has a EnumWindows function to enumerate open windows. It takes the EnumWindowsProc callback function as a parameter.
EnumWindows knows you need these windows, but doesn’t know why.
In addition, for some important reason, she does not want to give you a ready-made list at once.
But with the help of callback, it will hand over everyone one by one. For each detected window, your EnumWindowsProc function is called so that you can figure it out with this window yourself. And here, if the callback function is called, it will only be while EnumWindows is running, and not sometime later.


Answer 2

Offtopic to demonstrate the idea with a queue.

Here is a slightly modified example of BlockingQueue from Javadoc :
One thread in an infinite loop inserts into the queue, the other fetches. The number reader is practically not blocked (only for the duration of the call to queue.take () ). The Observer or any other callback will make it wait until this same callback completes before reading the next number.

class Producer implements Runnable {
  private final BlockingQueue queue;
  Producer (BlockingQueue q) {queue = q; }
  public void run () {
    try {
     while (true) {queue.put (produce ()); }
    } catch (InterruptedException ex) {... handle ...}
  }
  Number produce () {
    // reads another number from point
    // or as much as needed to calculate the average and return it
    Number x = ...;
    return x;
  }
}
class Consumer implements Runnable {
  private final BlockingQueue queue;
  Consumer (BlockingQueue q) {queue = q; }
  public void run () {
    try {
      while (true) {consume (queue.take ()); }
    } catch (InterruptedException ex) {... handle ...}
  }
  void consume (Number x) {
    // send a number to sockets
  }
}
class Setup {
  void main () {
    BlockingQueue & lt; Number & gt; q = new SomeQueueImplementation ();
    Producer p = new Producer (q);
    Consumer c1 = new Consumer (q);
    Consumer c2 = new Consumer (q);
    new Thread (p) .start ();
    new Thread (c1) .start ();
    new Thread (c2) .start ();
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions