Results 1 to 4 of 4

Thread: Work with TIME

  1. #1
    Maxwell is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Work with TIME

    Hey

    I have an object running on a server, and i want to know if 10 minutes have passed since the last interaction with the object. So, how can i compare if 10 minutes have already elapsed ?

    my doubt is: the object is running on the server, so how can i make comparation at real time ?

  2. #2
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    Can you modify the said object?
    If yes, then during interaction, as the client you can do something like
    setLastInteractionTime(System.currentTimeMillis()) ;
    to set the last time you interacted with it.
    And then when you get the object back do a comparison between
    client's current time and getLastInteractionTime() return.

  3. #3
    Maxwell is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Yes i can, but my goal is to see "automatically" if 10 minutes have passed.

    For exemple: for (each 10 minutes passed) {do something}

    how can i do that ?

  4. #4
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    Do you know the Listener design pattern (ever used keyListener??)

    Essentially your object needs to be the Source of events, and it will fire those events every 10 min since last interaction. You'll still need to have something like setLastInteractionTime.

    So anyway, the object is a source of events, inside the object there will be a thread (or maybe the thread should be some dispatcher-type class).
    The thread will be used for polling the object for its time, on some specified time interval. So your design will be something as follows:

    1. There is a dispatcher class which implements Runnable and has a thread in it.
    2. Objects are sources of tenMinPassedEvent, which say, passed the name of the said object to whatever implement's that object's listener interface.
    3. What's happening inside the dispather is something like this (in semi-pseudocode):
    Java Code:
    public void run() {
            Thread current = Thread.currentThread();
            while (current == instanceVarThreadOfDispatcher) {
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    
                    e.printStackTrace();
                }
                pollEveryObject(); //get every object's setLastInteractionTime here, and compare it with current time. If 10 min or more passed, the object generates a tenMinPassedEvent, which is propogated to all other objects which implemented it's Listener interface.
               
        }
    Will be accurate to 5ms time, on Thread.sleep(5) call.

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  2. webstart link some time doesn't work after Java upgrade
    By Richard Gillespie in forum Advanced Java
    Replies: 0
    Last Post: 12-21-2010, 04:01 PM
  3. calculate time diff for particular time period
    By baktha.thalapathy in forum New To Java
    Replies: 2
    Last Post: 05-24-2010, 04:10 PM
  4. Class Time - represents time of day
    By verbazon in forum New To Java
    Replies: 1
    Last Post: 04-13-2009, 01:06 AM
  5. Replies: 21
    Last Post: 08-01-2008, 11:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •