Results 1 to 4 of 4
  1. #1
    bpx95 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Question Working with threads.

    Okay, I'm trying to set a cool little animation.
    Basically, when the user presses down space, a boolean "req" is set to true, and a thread is started. The run() method is in the same class. The run method consists of a while loop which runs as req == true. When the user released the space bar, req is set to false, which I hoped would end the while loop, but instead it continues to loop. I used some System.out.println(req) tests along the way, and found that the thread thinks that req is set to true the entire time, even after releasing the space bar. How can I make it so that the while loop stops running after the space bar is released. (And yes, there has to be a while loop).

  2. #2
    bpx95 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    I... just figured out.... what happened.... I think I have a concussion... because I face palmed so hard.... I honestly, don't want to say what I did wrong, but trust me, it was stupid. But yeah, it works flawlessly now!

  3. #3
    Stamoulohta is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    I think threads in java have their own scopes and that's why you get this error. Try checking the req variable directly from the superclass like:
    Java Code:
    while(VariableClass.getReq()){
    // Record the animation
    }
    I think this may work...

  4. #4
    bpx95 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    Thanks for replying, but as I said, I figured it out. It was a really silly mistake, that only someone like me would make. I do these kinds of things all the time. In fact, I've made this exact same typo about 6 times tonight.

Similar Threads

  1. Replies: 2
    Last Post: 01-04-2011, 04:30 AM
  2. Threads
    By Tanuck in forum New To Java
    Replies: 5
    Last Post: 09-21-2010, 02:44 AM
  3. GUI and Threads
    By rp181 in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-10-2009, 08:39 PM
  4. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  5. Threads
    By one198 in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-20-2007, 06:15 PM

Tags for this Thread

Posting Permissions

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