如何在多线程应用程序中获取作业执行持续时间的准确时间戳?

发布于 2024-11-05 09:25:09 字数 1463 浏览 0 评论 0原文

如何将时间戳添加到多线程程序中,其中我有:

  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

咋地 2024-11-12 09:25:09

您应该使用 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.

澉约 2024-11-12 09:25:09

首先,发布更多代码,以便我们可以看到您真正在做什么以及您对日期格式的处理等等。

然后:SimpleDateFormat 的 Javadoc 说:

同步

SimpleDateFormat 不是线程安全的。
用户应该创建一个单独的
每个线程的实例。

这可能会导致您所看到的情况。

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:

Synchronization

SimpleDateFormat is not thread-safe.
Users should create a separate
instance for each thread.

Which may cause what you see.

匿名的好友 2024-11-12 09:25:09

使用 java.lang.System.nanoTime() 来测量经过的时间 - 它是您可用的最准确的计时器。不过,它是一个任意计数器(它并不代表时间,它只是计算纳秒),因此您需要像这样使用它:

long t1 = System.nanoTime(); 

// do something

long t2 = System.nanoTime();

long elapsedTime = t2 - t1; // in nano-seconds, so remember to scale!

有一个很好的线程 这里深入探讨 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:

long t1 = System.nanoTime(); 

// do something

long t2 = System.nanoTime();

long elapsedTime = t2 - t1; // in nano-seconds, so remember to scale!

There's a good thread here that goes into deep technical detail on the nanoTime call.

忆梦 2024-11-12 09:25:09

太长了;博士

Instant.now()       // Current moment in UTC
       .toString()  // Generate string in standard ISO 8601 format.

2018-01-23T04:52:47.830065Z

对于经过的时间:

Duration.between( instantThen , Instant.now() ) 

java.time

现代方法使用 java.time 类,而不是麻烦的旧遗留日期时间类,例如 Calendar.

即时 类代表 UTC 中时间轴上的时刻,分辨率为 纳秒(最多九 (9) 位小数)。

Instant instant = Instant.now() ;  // Capture current moment in UTC.

虽然Instant可以表示以纳秒为单位的值,但它在捕获当前时刻方面有局限性。

  • 在 Java 8 中,仅捕获最多毫秒的当前时刻。
  • 在 Java 9 中,新的 Clock实现现在以更精细的粒度捕获当前时刻。

在 macOS Sierra 上的 Java 9.0.1 中,我看到以微秒捕获的当前时刻。

2018-01-23T04:52:47.830065Z

以标准 ISO 8601 格式生成字符串上面,只需调用toString()即可。

String output = Instant.now().toString() ;

我建议使用 UTC 中的 Instant 和标准格式来满足您的日志记录需求。但如果您坚持不这样做,请参阅 DateTimeFormatter & ZonedDateTime 类。

如果您要计算经过的时间,请使用 Duration 类。

Duration d = Duration.between( instantThen , Instant.now() ) ;

要报告,请调用 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

Instant.now()       // Current moment in UTC
       .toString()  // Generate string in standard ISO 8601 format.

2018-01-23T04:52:47.830065Z

For elapsed time:

Duration.between( instantThen , Instant.now() ) 

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).

Instant instant = Instant.now() ;  // Capture current moment in UTC.

While an Instant can represent a value in nanoseconds, it has limitations is capturing the current moment.

  • In Java 8, the current moment is captured only up to milliseconds.
  • In Java 9, a new 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.

2018-01-23T04:52:47.830065Z

To generate a String in standard ISO 8601 format seen above, simply call toString().

String output = Instant.now().toString() ;

I suggest using the Instant in UTC with standard formatting for your logging needs. But if you insist otherwise, see the DateTimeFormatter & ZonedDateTime classes.

If you are calculating elapsed time, use Duration class.

Duration d = Duration.between( instantThen , Instant.now() ) ;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文