0

over the years I always encountered these kind of problems, and I've never solved them the way it felt "right".

imagine I have to implement a function/method to judge level of a river.the int river() gives us int from 0 to 10 , which means

0 being no water passed at all

, and

10 means near-over-flow

normally I would take the output for a few sec/min and then grade it to some groups such as empty/ half-full / full

if time is a free option here, how would you collect the output of river(), and judge after a few sec/min ? or even is it correct and reliable to use Time as a parameter in these kind of problems. Im asking for an Idea of algorithm to solve these type of questions. Respect

Edit1: bold part is my main question

Edit2: by Time, I mean implementing a Runnable and call it every 10 sec

Shervin
  • 233
  • 1
  • 13

1 Answers1

0

If I understood your question in the right way, you would have two seperate programs (or threads at least). One gathers information, the other interprets the information.

The data-gatherer runs non-stop and saves the data in a defined interval (for a river, maybe every 5 minutes). It stores the data in any kind of persistence layer (everything is possible from a simple array to a full-blown database)

The data-interpreter then asks the gatherer for that persistent data or gets it from the persistence layer directly if possible and then interprets it. The interpreter can run at any time you want and will always get the most recent information when it asks the gatherer.

Edit: to tackle your edits: Read this Java Thread every X seconds and that Accuracy of ScheduledExecutorService on normal OS / JVM. It should answer all your questions.

In short: It will be slightly inaccurate, but i think thats close enough for 99% of all the usecases.

Community
  • 1
  • 1
markusthoemmes
  • 3,080
  • 14
  • 23
  • I think it would make more sense if the data-interpreter got the data directly from the persistence layer. The data-interpreter and data-gatherer would then be independent from each other. – abl Feb 08 '14 at 15:58
  • @Merguez you got it right with a slight modification, data-gatherer runs non-stop but the data is not saved. I guess it would be more precise to rename data-gatherer to data-provider. I hear you are suggesting array or db implementation. why not the 'time" ? – Shervin Feb 08 '14 at 15:59
  • @Shervin then why is the data not saved? To be honest i didn't understand what you meant by 'Time' in this context. – markusthoemmes Feb 08 '14 at 16:01
  • the data is not saved by the river() as in my question, it simply provides different int over time. I (or we!) have to implement something to collect these data and judge it over time. I brought time up for this exact usage, to call the river() every 10 sec ie. – Shervin Feb 08 '14 at 16:04
  • @Shervin So then why is it a problem to store the data in an array in the interpreter and then after enough data is there to interpret it? – markusthoemmes Feb 08 '14 at 16:07
  • @Merguez there is no problem, but I feel Time is not the right approach here, I dont have anything to support my feeling. So you would approach this solution, so Time, to be specific in Time in Java is reliable to count on ? – Shervin Feb 08 '14 at 16:11
  • @Shervin check this out http://stackoverflow.com/questions/3541676/java-thread-every-x-seconds – markusthoemmes Feb 08 '14 at 16:12
  • I think it is not clear whether you want to evaluate the data over a period of time (say, the last 10 minutes) or just ask for the data periodically and do something with it instantly. If you want to evaluate data over a period of time then you absolutely need to save the data (at least temporarily) in some kind of storage, be it an object or a database. – abl Feb 08 '14 at 16:15
  • @Shervin If my link answers your question, yes ;). – markusthoemmes Feb 08 '14 at 16:16
  • @abl ofcourse I need to save it, no problem there, I asked about reliability of time in this matter. – Shervin Feb 08 '14 at 16:16
  • Sorry, I didn't understand the question. When you say 'time', you mean the class java.sql.Time? @Shervin – abl Feb 08 '14 at 16:20
  • @Shervin a combination of my link above and that: http://stackoverflow.com/questions/14255324/accuracy-of-scheduledexecutorservice-on-normal-os-jvm should perfectly solve your problem and answer your questions – markusthoemmes Feb 08 '14 at 16:24
  • @Merguez that's something, it says 5 minutes a month. will you provide/edit answer about this so that I can accept it – Shervin Feb 08 '14 at 16:30
  • @Shervin there we go. – markusthoemmes Feb 08 '14 at 16:39