• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

timing in Java

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following problem:
I need a clock, which first to take the value of the system time,
and then to be increased every second; I tryed to do this with a thread, but after a couple of minutes, there are differences between my clock and the system clock; how can I do to eliminate this difference ?
the run() method has the following code
public void run()
{

Calendar c= Calendar.getInstance();
while (true) {
try {

Thread.sleep(1000);
c.add(Calendar.SECOND,1);

jLabel1.setText(c.getTime().toString());
jLabel2.setText(Calendar.getInstance().getTime ().toString());
} catch(InterruptedException interruptedexception){}
}
}
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't really understand why you can't just read the system time each time your thread wakes up. Why would you need to keep a second copy of the time when there is already one in the system ?
 
Ana Mihailescu
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if fact, we have an applet which retrieve the time of a database
and after a while uses it; I don't want to connect to the database again, and I want to continue increasing the time from the database in the applet; but it seems that after a while some delays occur ...
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you say Thread.sleep(1000) you can never be certain that the
thread will start executing exactly after 1000 ms.
thats why there is a delay after some time.
Veena Rani
[This message has been edited by veena rani (edited May 08, 2001).]
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you can read system time from the applet, if that helps.
 
Ana Mihailescu
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Veena ,
I think you are right, I will try to do like this:
when I read the time from the database I make the difference between that time and the system time, and then , when I need again the time of the database, I will use this number and the system time.
Thanx for participating !
Ana
 
reply
    Bookmark Topic Watch Topic
  • New Topic