java实现时间同步,java中的时间同步

Inside a for-loop I'm controlling the simulation-step-based traffic simulator SUMO by retrieving and processing information of vehicles. To make sure that my program simulates in "real-time" (1 simulation-step = 1 second) I want to sleep my program after the processing phase until the next time step begins. To get better results I'm calculating the time stamp based on a initially taken reference time stamp.

The loop looks like this:

System.out.println("start of traffic simulation ...");

for (int i = 0; i < stepCount; i++)

{

System.out.println("step: " + i);

// set before timeStamp

beforeTimeStamp = System.currentTimeMillis();

if (firstStep)

{

// get reference timeStamp

referenceTimeStamp = beforeTimeStamp;

firstStep = false;

}

else

{

// get next vehicleVector

vehicleVector = masterControl.traCIclient.simulateStep();

}

// process vehicleVector

// set after timeStamp

afterTimeStamp = System.currentTimeMillis();

processingTime = afterTimeStamp - beforeTimeStamp;

// calculate sleepTime

sleepTime = referenceTimeStamp + ((i + 1) * 1000) - afterTimeStamp;

// sleep for sleepTime ms

Thread.sleep(sleepTime);

}

System.out.println("end of traffic simulation ...");

Here's the output of some variables:

step: 0

beforeTimeStamp 1252317242565

reference time: 1252317242565

processing time: 394

test time: 1252317243565

afterTimeStamp 1252317242959

sleepTime: 606

step: 1

beforeTimeStamp 1252317242961

processing time: 665

test time: 1252317244565

afterTimeStamp 1252317243626

sleepTime: 939 (exspected: 1000 - 665 = 335)

As you can see the sleep time is only correct for the first simulation step. I have no clue what might me going wrong here. Does anybody has an idea?

BR,

Markus

解决方案

Why not sleep for 1000 - processingTime ? It would be the closest you can get to a correct answer.

Your solution only works on the first step because it's only correct for the first step. You assume that you will start your processing for each step at referenceTime + (step * 1000), but you are not taking overhead (thread sleeping, prints, garbage collection) into account.

Print out referenceTimeStamp + ((i + 1) * 1000) - beforeTimeStamp to see what I mean

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java NTP时间同步程序改成服务模式可以通过以下步骤实现: 1. 编写一个Java类来启动时间同步程序,并保证它一直运行。可以使用Java的Timer类来设置定时任务,例如每隔一段时间就执行一次时间同步。 2. 将Java类打包成一个可执行的jar文件,并使用Java Service Wrapper(JSW)等工具将其包装成一个Windows服务或Linux服务。 3. 在Windows系统,可以使用sc命令将jar文件注册为一个Windows服务,例如: ``` sc create ntpservice binPath= "C:\path\to\java.exe -jar C:\path\to\ntpsync.jar" start= auto ``` 这将创建一个名为"ntpservice"的Windows服务,该服务会在系统启动时自动运行,并且会使用Java命令运行jar文件。 4. 在Linux系统,可以使用systemd等工具将jar文件包装成一个systemd服务单元,例如: ``` [Unit] Description=NTP Time Sync Service [Service] ExecStart=/usr/bin/java -jar /path/to/ntpsync.jar Restart=always User=ntpsync [Install] WantedBy=multi-user.target ``` 这将创建一个名为"ntpsync.service"的systemd服务单元,该服务会在系统启动时自动运行,并且会使用Java命令运行jar文件。 5. 启动服务并测试时间同步功能。在Windows系统,可以使用services.msc界面或net start命令启动服务;在Linux系统,可以使用systemctl start ntpsync.service命令启动服务。 注意:在服务模式下运行Java程序需要注意内存占用和异常处理等问题,以保证服务的稳定性和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值