To be honest you are doing a couple of things wrong. One thing is definitely that you're not protecting your "daysList" data structure from concurrent access from multiple threads. Another is that your loop can potentially run forever (actively) if it fails to find a task that needs to be returned. Third of all is that your search is very inefficient. It's much more efficient to have a queue of tasks sorted by the reminder time. Then all you need to do is check once a minute the first item in the queue whether its time has come or not. Or even set the timer to alarm you when the first item in the queue becomes active. Of course then you need to reset the timer if anything in the queue changes. There is no need nor any benefit from using threads in this situation. Even without any special treatment you don't need any extra threads -- once a minute go through all the items in daysList and check whether any of them need reminding. Assuming you have 10-ish or even 100-ish items in the list, it should be fast enough to not stall your UI.
Finally your last problem (which is the only one related to the future itself) is that you start a task and immediately ask for its result. If the result is not ready yet, the calling thread will be blocked until the result becomes available. Thus your watcher is never used. If you remove line #3 from your code, the program will start working.





Reply With Quote

Bookmarks