Re: Time transitions in Java
Matt Humphrey wrote:
Matt Humphrey wrote:
<omkar.tilak@gmail.com> wrote in message
news:1161794019.767092.249050@b28g2000cwb.googlegroups.com...
I am writing a java program which receives messages (over TCP socket)
from other java program. However, program need to do different
activities depending upon the message received. Program has a variable
called as prog_state. When prog starts, it is initialized to 'Start'.
Now in 'Start' state, if program receives message m1, it should change
its state to 'A' (i.e. merely assign value 'A' to prog_state'
variable). In the same start state, it it receives message m2, it
should change state to 'B'. However, if prog remains in 'Start' state
for more than certain time (say 5 seconds), it should automativally
change its state to 'C'. This is similar to FSM specification but it
has a time transition. I am having trouble in simulating the time
behavior (automatically transit after certain time period while still
being in a position to accept message during that period). Any
suggestions / code snippets in this regard will be of great help.
Thanks and regards
When you initialize your machine to the start state, start a 5-second
timer.
When you transition to A or B, turn off the timer. Otherwise, when the
timer goes off, transition yourself to C. I'm sure this exercise has to
do
with synchronization because you have to ensure that the arrival of the
next
message is clearly distinct from the activation of the timer.
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
<omkar.tilak@gmail.com> wrote in message
news:1161797544.619036.32230@f16g2000cwb.googlegroups.com...
Thanks for the quick reply Matt. But can u show me a code snippet ... I
tried to use Java Timer but I couldn't quite figure out the exact usage
.... also, the timer instruction may be a blocking instruction ... so
while thread is executing the timer, it may not be able to receive
messages .... maybe multiple threads is the answer ... but I'm having
trouble in putting everything together ... regards
First, please don't top-post. Put your reply after prior comments so that
its easier to read your comments in context. (You're using Google, which
has trouble with this concept.)
java.util.Timer is incredibly easy to use--read the API documentation for
exact usage. You can get them at http://java.sun.com/j2se/1.5.0/docs/api/
or if you dig around download them.
Create a TimerTask via anonymous subclass (needed to override the run
method)
TimerTask myTask = new TimerTask () {
public void run () {
// the code in here will run when the time expires.
}
};
// Create the timer and schedule the task for 5 seconds ahead.
Timer timer = new Timer ();
timer.schedule (myTask, 5000L);
If you get your events (A, B) do timer.cancel () or myTask.cancel() to
cancel the timer.
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
Hi Matt,
thanks for all the help. There is still one small issue though. Here
is my code
class tcpserver
{
String sentence;
String reply;
String state = "START";
public static void main(String args[]) throws Exception
{
Timer timer;
class RemindTask extends TimerTask {
public void run() {
System.out.format("State changed to C");
timer.cancel(); //Terminate the timer thread
}
}
// Create the timer and schedule the task for 5 seconds ahead.
ServerSocket welcome = new ServerSocket(2000);
while(true)
{
Socket connection = welcome.accept();
BufferedReader inClient = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
DataOutputStream out = new
DataOutputStream(connection.getOutputStream());
timer = new Timer();
timer.schedule(new RemindTask(), 5000);
while(timer.isRunning())
{
sentence = inClient.readLine();
timer.cancel();
state = sentence;
System.out.println("State changed to" + sentence);
}
}
}
}
I need to read from socket (inClient.readLine() ) only when the timer
is running (or while the RemindTask is not scheduled). However, Timer
class has no method to do this stuff. Do u think that using javax.swing
Timer might help here ... regards