This discussion is archived
5 Replies Latest reply: Mar 24, 2013 7:53 AM by rp0428 RSS

What approach to use to solve this kind of problem

meandmycode Newbie
Currently Being Moderated
Hi,

I want to know when the two measurements have reached a specifc value. Lets say value1 = REBOOT and value2 = READY
I will poll these two measurements on a server in a loop. These two values change state. So when I values changes I need to be notified and compare to wanted value.
If both wanted values are reached I will stop polling. Poll will end when I have a timeout set ( so I will do this poll for a while and if not successful within a time I will exit).

I would be very happy if you could point me in the right direction.

br,

//mike
  • 1. Re: What approach to use to solve this kind of problem
    EJP Guru
    Currently Being Moderated
    I don't understand the question. You've completely defined a solution. All you have to do now is code it. What's the problem exactly?
  • 2. Re: What approach to use to solve this kind of problem
    meandmycode Newbie
    Currently Being Moderated
    Hi,

    This is my first try:

    Here are my specific questions:

    - How can I get a timer into this? So that I run the checks for a max amount of time.
    - The check for state and progress takes different amount of time to get result. How can I wait for state and progress to return?

    Hope I am more concrete here.

    br,

    //mike

    public class UpgradeCOnfirm implements Runnable {
              
              private boolean timedout = false;
              private boolean condition = false;
              
              private String state;
              private String progress;
              
              //input
              private long timeout;
              private String expectedState;
              private String expectedProgress;
              

              public UpgradeCOnfirm(long timeout,String expectedState, String expectedProgress){
                   this.timeout = timeout;
                   this.expectedState = expectedState;
                   this.expectedProgress = expectedProgress;
              }

              @Override
              public void run() {
                   
                   while(!timedout || !condition){
                        
                        //kick of in separate thread,
                        setState(checkValueForState());
                        
                        setProgress(checkValueForProgress());
                        
                        //Wait
                        isConditionFullfilled();
                        
                        
                        
                   }
                   
              }
              
              public void isConditionFullfilled(){
                   if(expectedState.equalsIgnoreCase(getState()) && expectedProgress.equalsIgnoreCase(getProgress())){
                        condition = true;
                   }
                   
              }

              public String getState() {
                   return state;
              }

              public void setState(String state) {
                   this.state = state;
              }
              
              public String getProgress() {
                   return progress;
              }

              public void setProgress(String progress) {
                   this.progress = progress;
              }
         
              
              
         }
  • 3. Re: What approach to use to solve this kind of problem
    rp0428 Guru
    Currently Being Moderated
    >
    I want to know when the two measurements have reached a specifc value. Lets say value1 = REBOOT and value2 = READY
    I will poll these two measurements on a server in a loop. These two values change state. So when I values changes I need to be notified and compare to wanted value.
    If both wanted values are reached I will stop polling. Poll will end when I have a timeout set ( so I will do this poll for a while and if not successful within a time I will exit).
    >
    Well, you sound confused.

    You say you want to 'poll' these two measurements' but then you say just the opposite: 'when I values changes I need to be notified'.

    Either you 'poll' and determine yourself if the values have changed or you 'listen' and get notified when a value changes. Why do you think you need to do both.

    If YOU want to control when the checking is done then you 'poll'; since you can control when the polling is done.

    If you don't care how often the checking is done then you 'listen'; the other process might notify you every millisecond or every two weeks or may not notify you at all if no change occurs.

    Polling can be very inefficient if you have no idea how often, or when, the changes might happen since you will check many times and not really find out anything useful.

    Listening is used to get your attention when something of interest happens. Until you get notified you can spend all of your time doing something useful.
  • 4. Re: What approach to use to solve this kind of problem
    meandmycode Newbie
    Currently Being Moderated
    Hi,
    (
    Yes I am confused :-) and that is why I needed some clarification like you gave. Thanks.
    I have decided to use polling since my program needs to wait for the state of these two values ( or we timeout) before my program continues.
    So does my approach look ok?

    How about my questions on the code:

    - How can I get a timer into this? So that I run the checks for a max amount of time.
    - The check for state and progress takes different amount of time to get result. How can I wait for state and progress to return?

    br,

    //mike
  • 5. Re: What approach to use to solve this kind of problem
    rp0428 Guru
    Currently Being Moderated
    >
    I have decided to use polling since my program needs to wait for the state of these two values
    >
    Reread my reply.

    'Polling' is NOT waiting. If you want to wait you activate a 'listener' and then, if you want, sleep until the listener gets notified and wakes you up.
    Just set your time for the max that you want to wait. If you don't wake up before the end of the time then you exit or whatever.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points