Methods Running in Parallel

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 the information in that file in a local variable. It is a PedidosController class responsible for taking the information of the variable contained in PedidosListener and doing the processing of the values from there.

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

    
asked by anonymous 31.10.2015 / 04:20

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 going to be enough. You must register these objects as listeners of the event with the object that notices that such an event occurred. An example, in the case of Android, is when you make botao.setOnClickListener (yourListener), where yourListener is a copy 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 fetch from its internal list of interested listeners to know that there was a click and call the OnClick method of each one of them.

As you can see, there is no magic: someone has to call his method. In the case of the OnFileContentChangedListener, there will have to be an object that registers 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 is still much better than every interested object to be doing their own polling. In the real world, many events of this type work through signals, as polling is not very efficient.

In short: your listener will not stand by waiting. He will register as a listener and move on with life. Who will be responsible for advising when the time comes is the "observer". On how to implement a file watcher, check the implementations of the WatchService (Java 7) interface. If you need more information, I'll add it when I'm on the PC.

    
31.10.2015 / 04:44
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 forms you simply implement the run () method with your logic and invoke method start () of your thread. There is a complete tutorial on caelum's website link

If you have any questions, please let us help you.

    
31.10.2015 / 04:49