Results 1 to 16 of 16
  1. #1
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default Thread won't stop

    Hi all.

    I'm trying to do a long intensive task while updating a JLabel in the GUI.
    On a certain point, the Thread needs to stop.
    The problem is that it doesn't stop, it keeps updating the JLabel.

    Java Code:
    public void run() {
      while(!threadDone) {
        for(int i = 0; i < 100000; i++) {
          model.getLabel().setText(Integer.toString(i));
          if(i == 50000) {
            threadDone = true;
          }
        }
      }
    }
    After 50000 is passed, it keeps updating.
    Any help is appreciated.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You might have to post more code. A SSCE.

  3. #3
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    This is my program but simplified.

    Pseudo code:

    Java Code:
    For-loop (1 to 5)
      For-loop (array of 62)
        For-loop (array of 62)
          For-loop (array of 62)
            For-loop (array of 62)
              For-loop (array of 62)
    The main problem is that, the code around it works just fine, only the while loop isn't stopped when setting threadDone to true.

  4. #4
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Is it stopping when i is 100000?

  5. #5
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    No, it just keeps repeating the whole process because of the while loop.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Like I said, post a self compiling runnable example that demonstrates this problem.
    Also does the update go on until 100000?

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Don't bother. You set the flag but you don't break the for loop.

  8. #8
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    It's quite a big example if i need to to post everything here.
    I got the run() method, and 6 loops in eachother.

    Java Code:
    run(while(loop(loop(loop(loop(loop(loop)))))));
    All the loops execute fine, only the while always stays true so eveything repeats over and over again.

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Read my latest response. You set the flag but the loops still run to completion. Better break them to force execution out of the while loop.
    Loop basics actually.

  10. #10
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    The loop is inside the thread so if the thread stop, the loop should stop, right?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Are you reading what r035198 is writing? You may wish to acknowledge his statements and why you choose to accept them or reject them.
    Last edited by Fubarable; 10-20-2009 at 06:24 PM.

  12. #12
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    I did read it and understand it.
    I will have to break the loop and then check if the thread needs to stop and if so, I need to stop the thread.

    Java Code:
    for() {
      for() {
        for() {
          if() {
            break;
          }
        }
        if(isBroken) {
          threadDone = true;
        }
      }
    }
    Since I have 6 loops, I will have to do this 6 times.

    I used a branching statement for the while loop, now it works.

    Thanks for your help.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You can break out the entire loop via a break to a label:

    Java Code:
      public static void main(String[] args) {
        int i = 0;
        int j = 0;
        int k = 0;
    
        done: 
        while (true) {
          for (i = 0; i < 10; i++) {
            for (j = 0; j < 10; j++) {
              for (k = 0; k < 10; k++) {
                if (i == 2 && j == 3 && k == 4) {
                  break done;
                }
              }
            }
          }
        }
    
        System.out.println(i + ", " + j + ", " + k);
      }
    Or even simpler, put the loops in a method and just call return.

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by Fubarable View Post
    Are you reading what r035198 is writing? You may wish to acknowledge his statements and why you choose to accept them or reject them.
    How dare you call me that?
    I'll have to report you to the *SPNEX.





    (*)Society for People with Names Ending with X

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  16. #16
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    why not just return out of the run method?

    Java Code:
    if(...)
    {
       return;
    }
    My Hobby Project: LegacyClone

Similar Threads

  1. Forcing a thread to stop
    By sukatoa in forum Threads and Synchronization
    Replies: 7
    Last Post: 07-17-2009, 06:41 AM
  2. Help to stop a thread
    By raghu_lzybns in forum New To Java
    Replies: 4
    Last Post: 07-09-2009, 04:39 PM
  3. [SOLVED] How to stop a thread
    By AlejandroPe in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 03:05 PM
  4. how to stop a thread
    By willemjav in forum Advanced Java
    Replies: 19
    Last Post: 09-10-2008, 07:11 AM
  5. The safe way to stop a thread
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:31 PM

Posting Permissions

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