×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Feb 2007
    Posts
    29

    exiting a loop / loop and thread.

    I am having a very strange problem. What I think is happening is that a buffer is filling up, because the phone cannot display the information fast enough and then my program becomes non responsive.

    Here is the Interrupt function.
    Basically what happens is you press "Start" and then the button alters state and turns into a "stop" button when you press stop the button reverts to a start button.


    Code:
     public void commandAction(Command command, Displayable displayable) 
       {
                  if(command == StartCommand)
           {
             MainForm.removeCommand(StartCommand);
             MainForm.addCommand(get_StopCommand());  
             run();
           }
           
           if(command == StopCommand)
           {
             MainForm.removeCommand(StopCommand);
             MainForm.addCommand(get_StartCommand());   
             stopServices();
           }
           
       }

    here is a snippet of the code

    Code:
     
    
        private void stopServices()
        {
            quit();   
        }
        
         public void run()
        {
          while(QUIT == false) {
                System.out.println("Recorder");
                try{
                        {   
    
                            System.out.println("pause1");
                            // Thread.sleep(1000);
                            System.out.println("pause2");
                            
                           
                        }
                 
                } catch (Exception ex) {
                    System.err.println("Error in recorder thread: " + ex.toString());
                }
            }
        }
        
          
        public void quit()
        {
            QUIT = true ;
        }
    I originally had this running in a thread, but to try to find out what was going on, I simplified it a lot.

    Is Thread.sleep() the best thing to do to get the thing to pause for an amount of time so that it prints out every second rather than continuously?

    I will have 3 threads in the future. One to get GPS coords , one to calculate a route and one to give the user instructions.

  2. #2
    Registered User
    Join Date
    Feb 2007
    Posts
    29

    Re: exiting a loop / loop and thread.

    The solution I came up with is to create a thread like this :
    Code:
    Thread thread = Thread(gg);
    gg is a object which implements Runnable.

    I then loop the run() method in the gg class. I also provide it with a quit() command as I did previously, bu tin the gg class itself. The while loop works in exactly the same way as in my question. QUIT is a boolean var which is set to false on the instantiation or init of an object.
    Code:
    run(){while(QUIT){/*do stuff*/}}
    Any other threads started by this one also need to be killed by dropping down into them via the quit() method.

    eg.
    Code:
    void quit(){gg.quit()}
    then in gg there will be another thread which quits the lower thread
    eg
    Code:
    void quit(){otherrunnable.quit(); QUIT = true;}
    To make the thread start
    Code:
    thread.start()
    To quit the thread, and thus stop everything looping.
    Code:
    gg.quit()
    Last edited by adamzieba; 2007-08-04 at 15:21.

Similar Threads

  1. possible loop in MMSPDUDecoder causing stuck thread!
    By carolang in forum General Messaging
    Replies: 0
    Last Post: 2006-12-15, 12:48
  2. Nokia 6620 crash in BT device/service loop
    By dcowing in forum Mobile Java Networking & Messaging & Security
    Replies: 7
    Last Post: 2005-02-08, 21:40
  3. please help... thread & active scheduler
    By suzuk in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2004-06-09, 14:43
  4. question about thread in game loop!
    By cach2008 in forum Mobile Java General
    Replies: 4
    Last Post: 2004-02-09, 01:52
  5. Thread management on 7650
    By shmoove in forum Mobile Java General
    Replies: 1
    Last Post: 2003-03-21, 16:20

Posting Permissions

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