Skip to content

Instantly share code, notes, and snippets.

@moea
Last active December 16, 2015 13:29
Show Gist options
  • Save moea/5442121 to your computer and use it in GitHub Desktop.
Save moea/5442121 to your computer and use it in GitHub Desktop.
package com.novoda.example;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class LoggingWorker implements Runnable {
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
private final int workerId;
public LoggingWorker(int workerId) {
this.workerId = workerId;
}
@Override
public void run() {
while(!Thread.interrupted()) {
Date now = new Date();
String formatted = DATE_FORMAT.format(now);
System.out.printf("%d: I did some work at %s%n", workerId, formatted);
String formatted_ = DATE_FORMAT.format(now);
if(!formatted.equals(formatted_)) {
System.out.printf("%d: Inconsistency! %s vs. %s - terminating%n", workerId, formatted, formatted_);
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
return;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment