长期以来,我使用来测量经过时间System.currentTimeMillis(),直到最近才发现我也可以使用nanoTime()。然而,一些结果不是我所期待的,因为nanoTime测量的时间从任意时间点,而不是01-01-1970为currentTimeMillis做。
返回最精确的可用系统计时器的当前值(以纳秒为单位)。
此方法只能用于测量经过的时间,与系统或挂钟时间的任何其他概念无关。返回的值表示自某个固定但任意时间以来的纳秒 (也许是将来的时间,因此值可能为负)。此方法提供纳秒精度,但不一定提供纳秒精度。不能保证值更改的频率。由于数值溢出,跨越大约292年(263纳秒)的连续呼叫中的差异将无法准确计算经过的时间。
例如,要测量某些代码执行所需的时间:
Run Code Online (Sandbox Code Playgroud)long startTime = System.nanoTime(); // ... the code being measured ... long estimatedTime = System.nanoTime() - startTime;
我对任意部分感到困惑。仅当两个呼叫的参考时间戳相同时,才能测量经过的时间。但是在API文档中不能保证。我认为这就是我提到的问题的原因。使用的时间是实现细节,因此我不能依靠它。
我想知道什么时候可以充分利用,nanoTime在什么情况下会出现严重错误。我想到的主要用例是:
nanoTime来自不同线程的时间戳nanoTime来自同一JVM的不同实例的时间戳(例如,思考,来自先前应用程序运行的序列化时间戳,或作为消息的一部分传递给另一JVM)nanoTime不同JVM实现中的时间戳在StackOverflow上有一些帖子可以解决部分问题,但是很大程度上没有选择时间:
尤其是最后一个链接清楚地表明,何时 nanoTime可以使用存在一些限制,但是在不完全了解这些限制的情况下,使用它似乎本质上是不安全的,因为基于它的功能可能会失败。
private Timestamp timestamp = new Timestamp(System.nanoTime());在创建对象时,我正在我的一个实体中执行操作.
如果我system.out时间戳,我得到的价值就像;
2282-08-24 11:25:00.506
2286-04-16 01:47:35.882
Run Code Online (Sandbox Code Playgroud)
这有什么不对?
System.currentTimeMillis()给出正确的日期,但我需要更准确.这可能有什么问题?
Java:我在使用System.currentTimeMillis()函数时遇到问题
我正在使用System.currentTimeMillis()在foor循环问题中生成唯一值,循环执行得太快,System.currentTimeMillis()给我重复值.
我如何生成确定的唯一值.
for(int a=0;a<=10;a++){
System.out.println(System.currentTimeMillis())
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过跟踪,但它也不是生成唯一数字的特征
System.currentTimeMillis()+Math.random()
Run Code Online (Sandbox Code Playgroud) 我希望计算从Java中特定时间开始的毫秒数.
它System.currentTimeMillis();用于开始时间的经典方式,然后再使用前一个来获取经过的时间.我希望做类似的事情,但不要依赖系统时间.
如果我依赖系统时间,程序的用户可以操纵系统时钟来破解程序.
我尝试使用类似于以下的代码:
int elapsed = 0;
while (true) {
Thread.sleep(10);
elapsed += 10;
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,但是在计算机滞后然后锁定一两秒的情况下,它不太可靠.
任何人的想法?
我注意到Akka中的scheduler.schedule不适用于小于10ms的调度.例如,我在下面尝试过,我每隔10ms只收到一条消息.是否有可能减少这种情况,即增加频率?
谢谢Des
akka.actor.ActorSystem("Test").actorOf(Props(new akka.actor.Actor {
val system = context.system
import system.dispatcher
system.scheduler.schedule(0 milliseconds, 2 milliseconds, self, "Test")
def receive = {
case msg =>
println("Received[" + System.currentTimeMillis + "]: " + msg)
}
}), "Test")
Run Code Online (Sandbox Code Playgroud) 我如何使用它并为其添加过期时间,这意味着如果流中存在相同的值,我们仍然会收集它,因为在发出前一个重复值之后distinctUntilChanged()它的时间超过了毫秒。expiry
flow {
emit("A") // printed
emit("B") // printed
emit("A") // printed
emit("A") // NOT printed because duplicate
delay(5000)
emit("A") // printed because 5 seconds elapsed which is more than expiry
}
.distinctUntilChanged(expiry = 2000)
.collect {
println(it)
}
Run Code Online (Sandbox Code Playgroud)
我想打印这个:
A
B
A
A
Run Code Online (Sandbox Code Playgroud)
这是测试它的代码:
@Test
fun `distinctUntilChanged works as expected`(): Unit = runBlocking {
flow {
emit("A") // printed
emit("B") // printed
emit("A") // printed
emit("A") // NOT printed because duplicate
delay(5000)
emit("A") // printed because …Run Code Online (Sandbox Code Playgroud) 在详细讨论问题之前,我想先介绍一下问题的背景。基本上,我的代码看起来像
for(int i = 0; i < n; i++){
startTime = System.currentTimeMillis();
result = doSomething();
endTime = System.currentTimeMillis();
responseTime.add(endTime - startTime);
results.add(result);
}
print(results and responseTime);
Run Code Online (Sandbox Code Playgroud)
现在我想做的是将doSomething()作为 completableFuture 运行并获取上面提到的responseTime。但因为这样做是完全错误的——
for(int i = 0; i < n; i++){
startTime = System.currentTimeMillis();
resultCF = CompletableFuture.supplyasync(() -> doSomething());
endTime = System.currentTimeMillis();
responseTime.add(endTime - startTime);
results.add(resultCF); //result is to store the completableFutures
}
CompletableFuture.allOf(results.toArray(new CompletableFuture[results.size()])).join();
print(i.get() for i in results and responseTimes);
Run Code Online (Sandbox Code Playgroud)
因为它会破坏获取每个doSomething()的执行时间的目的。那么,有什么方法可以获得每个 completableFuture 的响应时间吗?另外,我需要在循环末尾包含 completableFutures( resultCF ) 的结果数组列表。
long startTime = System.nanoTime();
long startTimer = System.currentTimeMillis();
M = app.decriptare_simpla(C);
long endTime = System.nanoTime();
long stopTimer = System.currentTimeMillis();
//mesajul initial dupa decriptare
System.out.println("M : " + M.toString());
System.out.println("Decriptarea a durat: " + (endTime - startTime));
System.out.println("Decriptarea a durat: " + (stopTimer - startTimer));
Run Code Online (Sandbox Code Playgroud)
这给了我:
Decriptarea a durat: 14811776
Decriptarea a durat: 15
Run Code Online (Sandbox Code Playgroud)
我想问的是这两个数字是多少秒?我的意思是它们是 0.15、0.015、0.0015 ......?我想以这种方式打印它们,而不是作为一个long但不知道要添加多少个小数。另一个号码也有同样的问题。
下面的Java方法是指以打印数i由nLoopsPerSecond每秒次seconds秒:
public void test(int nLoopsPerSecond, int seconds) {
double secondsPerLoop = 1.0/(double)nLoopsPerSecond;
long startTime = System.currentTimeMillis();
long currentTime;
int i = 0;
while ((currentTime = System.currentTimeMillis()) < startTime + seconds*1000) {
System.out.println(i++);
while (System.currentTimeMillis() < currentTime + secondsPerLoop*1000);
}
}
Run Code Online (Sandbox Code Playgroud)
通过以下电话:
test(1000,1);
Run Code Online (Sandbox Code Playgroud)
我希望这种方法可以做System.out.println(i++);1000次,但我只有63次.
当我尝试使用此代码查看每个循环实际使用的秒数
public void test(int nLoopsPerSecond, int seconds) {
double secondsPerLoop = 1.0/(double)nLoopsPerSecond;
long startTime = System.currentTimeMillis();
long currentTime;
int i = 0;
while ((currentTime = System.currentTimeMillis()) < startTime + …Run Code Online (Sandbox Code Playgroud) java ×8
time ×6
akka ×1
asynchronous ×1
clock ×1
for-loop ×1
internal ×1
kotlin ×1
kotlin-flow ×1
loops ×1
nanotime ×1
random ×1
scala ×1
unique ×1
while-loop ×1