Modification Timestamps on Objects or Counters
Posted on 2004-10-22
1) I want to associate a modification time with my Objects.
2) These objects are not touched by other threads. They all run in the same thread.
3) But I need to know which one is the most recently modified.
4) I *cannot* have a situation where Object A and Object B return the same modification timestamp. Since everything is running in a single thread, the call to modify Object A must have occured either before OR after the call to modify Object B. It cannot have occured at the exact same time.
I am wondering if System.currentTimeMillis() is accurate enough? I need to know what I should use to set the modification timestamp. Object A.modified should NEVER equal Object B.modified, otherwise my program could produce wrong results.
So what's the best thing to use to set the timestamp?
hmm... I just think I answered my question partially while writing this... I guess a counter is a much better solution than a timestamp. So how would I implement a counter properly? Object A and Object B do not have any references to a global object where I can store the counter. They will usually be running in a multithreaded environment like a Servlet or JSP page. But they have no ability to access Servlet or JSP or any J2EE objects.
So where can I put a counter that I can access from Object A and Object B but where the counter cannot be influenced by other threads running in the same VM ?