I have a Server/Client application that also includes a timer. Communication is allowed between the server and client until a deadline time (using a Calendar
object) is reached. The reaching of this deadline is monitored in a separate thread, which is called from the server:
//create and run a timer on bid items
public static void startClock()
{
//thread to track item deadlines
DeadlineClock deadlineClock =
new DeadlineClock(deadline);
//begin running the clock
deadlineClock.start();
}
The deadline is then detected like so:
//inside DeadlineClock's run() method
//Retrieve current date and time...
Calendar now = Calendar.getInstance();
//deadline not yet reached
while(now.before(deadline))
{
try
{
//wait a second and try again
sleep(1000);
}
catch (InterruptedException intEx)
{
//Do nothing.
}
//Update current date and time...
now = Calendar.getInstance();
//run loop again
}
What I would like to do is have a way of detecting (in the Server) when the deadline has been reached in the DeadlineClock
, but I'm completely unable to find an implementable way of doing this, without completely duplicating the whole timing mechanism in the server.
From what I know, it would essentially take some kind of output or return of a value on the DeadlineClock
side, but I have no idea how this would be read in or detected on the Server-side, and this would also be difficult to scale if the program ever had more than one deadline involved.
My only other idea was to pass a boolean variable into the DeadlineClock
constructor, and then try and wait to detect if this changed, something like this (assuming that the variable's value was changed once the deadline was reached):
//create and run a timer on bid items
public static void startClock()
{
//initialise as false before items run
boolean deadlineReached = false;
//thread to track item deadlines
DeadlineClock deadlineClock =
new DeadlineClock(deadline, deadlineReached);
//begin running the clock
deadlineClock.start();
//monitor other thread for value change
while (deadlineReached != true)
{
//do nothing until changed
wait();
}
///////////////////////////
///CHANGE DECTECTED HERE///
///////////////////////////
}
This is pretty rough, but hopefully somewhere along the right lines. Can anyone suggest how I might be able to implement the functionality I'm after?
Thanks, Mark