如何在多线程应用程序中获取作业执行持续时间的准确时间戳?
如何将时间戳添加到多线程程序中,其中我有:
- J3 依赖于 J1 & J2
- J5 依赖于 J4
并获取每个作业(线程)执行的不同时间。
我有 6 个不同的文件:J1、J2、J3(我在其中加入了 J1 和 J2)、J4、J5(在其中我加入了 J4)和 J6(在其中我启动了所有线程)。
我添加了此功能:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:ms");
Calendar cal = Calendar.getInstance();
示例代码(一个线程)——
import java.io.*;
import java.*;
import java.util.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Job1 extends Thread{
String msg;
public void run()
{
System.out.println("Execution of job1 (addition job) started");
System.out.println();
System.out.print("3+2=" +(3+2));
System.out.println();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:ms");
Calendar cal = Calendar.getInstance();
}
Job1(String mg)
{
msg=mg;
}
}
但是,我为每个线程获得相同的时间。
更新
这是我得到的输出。
C:\Program Files\Java\jdk1.6.0_03\bin>javac threadcontainer.java
C:\Program Files\Java\jdk1.6.0_03\bin>java threadcontainer
J1->4719906194666
J2->4719906696464
Execution of job1 (addition job) started
3+2=5
J4->4719911159535
Execution of job4 (multiplication job) started
3*2=6Job 5 executing
J5-> 4719911737462
Execution of job2 (subtraction job) started
3-2=1Job 3 executing
J3->4719912405874
How to add timestamp into a multi threaded program where I have:
- J3 dependent on J1 & J2
- J5 dependent on J4
And get the different times of executions of each job (thread).
I have 6 different files of J1, J2, J3 (where I have joined J1 & J2), J4, J5 (where I have joined J4) and J6 (where I start all the threads).
I have added this function:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:ms");
Calendar cal = Calendar.getInstance();
Sample Code(of one thread) --
import java.io.*;
import java.*;
import java.util.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Job1 extends Thread{
String msg;
public void run()
{
System.out.println("Execution of job1 (addition job) started");
System.out.println();
System.out.print("3+2=" +(3+2));
System.out.println();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:ms");
Calendar cal = Calendar.getInstance();
}
Job1(String mg)
{
msg=mg;
}
}
But, I am getting the same time for every thread.
Update
This is the output I am getting.
C:\Program Files\Java\jdk1.6.0_03\bin>javac threadcontainer.java
C:\Program Files\Java\jdk1.6.0_03\bin>java threadcontainer
J1->4719906194666
J2->4719906696464
Execution of job1 (addition job) started
3+2=5
J4->4719911159535
Execution of job4 (multiplication job) started
3*2=6Job 5 executing
J5-> 4719911737462
Execution of job2 (subtraction job) started
3-2=1Job 3 executing
J3->4719912405874
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用 System.nanoTime() 更准确、更快。
日历是您可以创建的最昂贵的对象之一,并且使用日期格式可能效率非常低。 “:ms”没有做任何有用的事情,所以我怀疑你只有第二个解决方案。如果您想要毫秒,请按照 javadoc 使用“.SSS”。
You should use System.nanoTime() is much more accurate and much faster.
Calendar is one of the most expensive objects you can create and using a date format is likely to be very inefficient. The ":ms" doesn't do anything useful, so I suspect you only have second resolution. If you want milli-seconds use ".SSS" as per the javadoc.
首先,发布更多代码,以便我们可以看到您真正在做什么以及您对日期格式的处理等等。
然后:SimpleDateFormat 的 Javadoc 说:
这可能会导致您所看到的情况。
First, post more code, so that we can see what you are really doing and what you to with the date format and so on.
And then: Javadoc for SimpleDateFormat says:
Which may cause what you see.
使用 java.lang.System.nanoTime() 来测量经过的时间 - 它是您可用的最准确的计时器。不过,它是一个任意计数器(它并不代表时间,它只是计算纳秒),因此您需要像这样使用它:
有一个很好的线程 这里深入探讨 nanoTime 调用的技术细节。
Use java.lang.System.nanoTime() to measure elapsed time - it's the most accurate timer available to you. It's an arbitrary counter though (it doesn't represent a time as such, it just counts nanoseconds), so you'll need to use it like this:
There's a good thread here that goes into deep technical detail on the nanoTime call.
太长了;博士
对于经过的时间:
java.time
现代方法使用 java.time 类,而不是麻烦的旧遗留日期时间类,例如
Calendar.
即时
类代表 UTC 中时间轴上的时刻,分辨率为 纳秒(最多九 (9) 位小数)。虽然
Instant
可以表示以纳秒为单位的值,但它在捕获当前时刻方面有局限性。Clock
实现现在以更精细的粒度捕获当前时刻。
在 macOS Sierra 上的 Java 9.0.1 中,我看到以微秒捕获的当前时刻。
以标准 ISO 8601 格式生成字符串上面,只需调用
toString()
即可。我建议使用 UTC 中的
Instant
和标准格式来满足您的日志记录需求。但如果您坚持不这样做,请参阅DateTimeFormatter
&ZonedDateTime
类。如果您要计算经过的时间,请使用
Duration
类。要报告,请调用
Duration::toString
以标准 ISO 生成字符串8601 格式。关于 java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
, & ;SimpleDateFormat
。Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。
要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310。
从哪里获取 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time 。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
间隔
,YearWeek
,<代码>YearQuarter,以及更多 。tl;dr
For elapsed time:
java.time
The modern approach uses java.time classes rather than the troublesome old legacy date-time classes such as
Calendar
.The
Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).While an
Instant
can represent a value in nanoseconds, it has limitations is capturing the current moment.Clock
implementation now captures the current moment with a finer granularity.In Java 9.0.1 on macOS Sierra, I am seeing current moments captured with microseconds.
To generate a String in standard ISO 8601 format seen above, simply call
toString()
.I suggest using the
Instant
in UTC with standard formatting for your logging needs. But if you insist otherwise, see theDateTimeFormatter
&ZonedDateTime
classes.If you are calculating elapsed time, use
Duration
class.To report, call
Duration::toString
to generate a String in standard ISO 8601 format.About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.