Results 1 to 2 of 2
- 08-04-2010, 10:06 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 7
- Rep Power
- 0
Automating task using recursive threads
Hi All,
I have to write a program which reads from a text file sequentially some commands line by line. It then uses the read command information to make a call to another methods with respect to what command it has read from the file.
This is for automating the movement of a vehicle using a script file.
So my text/script file can be like the following:
START /*start of the file*/
Q /*turn left*/
A20 /*Accelerate for 20 seconds*/
W /*Make wheels straight*/
S /*stop*/
END /*end of the file*/
So I need a timer for addressing the command A20, since I have to accelerate the =vehicle for 20 seconds..
I want this thread which is reading from the file to wait but the background thread to run so the vehicle keeps on moving for the 20 seconds time. And only after the vehicle has finished movement, I want to read the rest of the file and interpret the other commands.
I had tried using java timer but ended up with the timer threads not getting killed at the end..
Any ideas on how to do this?
Thanks in advance
Jith
- 08-10-2010, 04:41 PM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
I would probably have the accelerate thread normally just wait, and then have the command thread notify it when you need to accelerate, and then have the command thread wait for a notify from the accelerate thread. Note, you need to use a common object for both wait's or you'll open a tiny window where a deadlock is possible.
The following pseudocode ignores InterruptedExceptions...it's not going to compile as is
Java Code:Object lockObj = new Object(); Thread accelThread; Thread commandThread; accelThread = new Thread() { public void run() { while(true) { synchronized(lockObj) { lockObj.wait(); ...accelerate... lockObj.notify(); } } } }; commandThread = new Thread() { public void run() { ...your parsing loop ...when it's time to accelerate synchronized(lockObj) { lockObj.notify(); lockObj.wait(); } ...continue parsing } };
Similar Threads
-
task
By boys21 in forum Advanced JavaReplies: 6Last Post: 05-28-2010, 01:59 PM -
Automating Web application using Java API
By BikuDAA in forum AWT / SwingReplies: 0Last Post: 04-27-2010, 08:27 PM -
Help with a task.
By checho in forum New To JavaReplies: 5Last Post: 01-14-2010, 11:29 AM -
Java Task
By Sokox in forum NetBeansReplies: 4Last Post: 12-13-2009, 09:57 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks