Methods Running in Parallel

Asked

Viewed 89 times

1

Hi.

I think this is going to be a very simple answer, but it’s something I still have doubts about how to do.

Let’s say I have a class PedidosListener responsible for reading a . csv file and storing that file information in a local variable. And a class PedidosController responsible for taking the information of the variable contained in PedidosListener and process the values from there.

My question is, how to make class PedidosListener read the file. csv continuously, and when he notices some change in the file, add the change to the local variable and send the information that there was change back to the class PedidosController without it stopping the execution of PedidosListener

2 answers

1


Listener is usually a type of interface implemented by object classes that respond to certain actions. If you have an Onfilecontentchangedlistener interface, for example, it will declare a method called Onfilecontentchanged. Anyone who wants to respond to file change events must be from a class that implements this interface. But that’s probably not gonna be enough. It is necessary to register these objects as listeners of the event with the object that realizes that such an event occurred. An example, in the case of Android, is when you do the.setOnClickListener(youListener), where youListener is an example of a class that implements Onclicklistener. What will happen is that the button, when receiving from the user interface manager a signal that there was a click on it, it will search in its internal list of listeners interested in knowing that there was a click and call the Onclick method from each of them.

As you can see, there is no magic: someone has to call his method. In the case of Onfilecontentchangedlistener, there will have to be an object that records file change listeners. This object will have to have some way of knowing when there was a change in the file to call the Onfilecontentchanged methods of the registered listeners. This may or may not be done by Polling (staying all the time going there and checking the modification date, for example). Even if it is, it’s still much better than every object interested to stay making your own Polling. In the real world, many events of this kind work via signals, because Polling is not very efficient.

In short: your Istener will not stand by and wait. He will register as a listener and move on with his life. Who will be responsible for warning when the time comes is the "observer". On how to implement a file observer, check the implementations of the Watchservice interface (Java 7). If you need more information, add it when you’re on PC.

  • 1

    Got it. Thanks a lot for your help. :)

1

You can start working with threads. There are two ways to create a thread, the first is to create an extended class of threads, the second is to create a class implementing Runnable, both forms work and both ways you simply implement the run() method with its logic and invoke the start() method of your thread. There is a very complete tutorial on the website http://www.caelum.com.br/apostila-java-orientacao-objetos/programacao-concorrente-threads/#17-1-threads

If you have any questions put there that we will help you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.