Last active
December 16, 2015 13:29
-
-
Save moea/5442121 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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