I have been tasked with creating a Java version of a C# SDK. Currently. I am working on a class that extends the C# System.ServiceProcess.ServiceBase but due to the difficulty of creating Windows services in Java I am focusing on some of the other methods in the class.
The current C# method I am attempting to replicate in Java looks as follows
private void StartProcesses()
{
// create a new cancellationtoken souce
_cts = new CancellationTokenSource();
// start the window timer
_windowTimer = new Timer(new TimerCallback(WindowCallback),
_cts.Token, 0, Convert.ToInt64(this.SQSWindow.TotalMilliseconds));
this.IsWindowing = true;
}
After analyzing this section of code I believe that it initializes a System.threading.Timer object that executes the WindowCallback function every SQSWindow milliseconds.
After reading through the java.util.concurrent documentation located
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/package-summary.html
I am unsure how I would replicate the C# functionality in Java as I cannot find the equivalent to the Timer functionality. The TimeUnit provided by the Java library appears to be only used for thread timeouts and not to issue recurring operations.
I am also curious as to the use of the CancellationTokenSource. If this object is meant to be queried to determine if the action is to continue, why is it not a primative such as a boolean? What additional functionality does it provide, and is there a similar construct in Java's multithreading model?