Results 1 to 2 of 2
  1. #1
    jitheshmenon is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default 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

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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

  1. task
    By boys21 in forum Advanced Java
    Replies: 6
    Last Post: 05-28-2010, 02:59 PM
  2. Automating Web application using Java API
    By BikuDAA in forum AWT / Swing
    Replies: 0
    Last Post: 04-27-2010, 09:27 PM
  3. Help with a task.
    By checho in forum New To Java
    Replies: 5
    Last Post: 01-14-2010, 12:29 PM
  4. Java Task
    By Sokox in forum NetBeans
    Replies: 4
    Last Post: 12-13-2009, 10:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •