Results 1 to 7 of 7
  1. #1
    Sebasnr is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Question Problem 'sleeping' Threads

    Hi guys, I'm trying to do a tiny spaceship game, but the problem is I want to sleep an enemy, but with the method sleep(int milliseconds) from Thread, it sleeps the "AWT-EventQueue-0" Thread (I do implement the MouseListener and MouseMotionListener Interfaces for move the spaceship). So, how can I sleep the enemy Thread instead the EventQueue?

    Thanks and sorry for my bad english.

    ----------

    Edit:

    Here is the code: http://www.mediafire.com/?f8upyaeo6738id6
    The names are mostly in spanish, but it's not very complicated so I think it's understandable
    Last edited by Sebasnr; 12-31-2011 at 03:30 AM.

  2. #2
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Problem 'sleeping' Threads

    Do you have more than one thread?
    If so, WHY?

  3. #3
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default Re: Problem 'sleeping' Threads

    Where do you attempt to sleep() the enemies?

    Currently the enemies are executed by an ExecutorService. I don't think you have much control over the thread in which they run. (That's what the Executor controls). But I'm no expert on threading! So maybe someone else can comment.

    Also the enemies have an hilo, but you aren't using that.

  4. #4
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default Re: Problem 'sleeping' Threads

    I closed the other thread (which duplicated this one, but without the link to the code). And moved jammas' response to here.

    There was a slight screw up as I closed the wrong thread, but hopefully that's sorted now.

  5. #5
    Sebasnr is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: Problem 'sleeping' Threads

    Thanks for closing the other thread, i was trying to edit it and I don't know why I open a new one,,,

    Quote Originally Posted by pbrockway2 View Post
    Where do you attempt to sleep() the enemies?

    Currently the enemies are executed by an ExecutorService. I don't think you have much control over the thread in which they run. (That's what the Executor controls). But I'm no expert on threading! So maybe someone else can comment.

    Also the enemies have an hilo, but you aren't using that.
    The code i've uploaded is a security-copy where all is running correctly, but I've tried to sleep it in "PanelJuego.actualizarEnemigos" (Creating a new Thread(enem[i]) and then calling Thread.sleep(100) when the spaceship crash with an enemy). I've also tried to create a Thread hilo = new Thread(this) in the Enemy constructor and then call the sleep method in "Enemigo.setSprite". Both had the same results, when I do a System.out.println(Thread.currentThread().getName( )), it prints "AWT-EventQueue-0"

  6. #6
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default Re: Problem 'sleeping' Threads

    Creating a new Thread(enem[i]) and then calling Thread.sleep(100) when the spaceship crash with an enemy
    Thread.sleep() will sleep the currently executing thread (the one doing the painting) so that's no good.

    I've also tried to create a Thread hilo = new Thread(this) in the Enemy constructor and then call the sleep method in "Enemigo.setSprite"
    But if you call setSprite() as a result of "if(validarColision(nave, enem[i]))" then the currently executing thread is also the event queue :(

    If anything it would have to be the run() method of enemigo that has to call sleep(). What you can do in the paint code is set some variable in the enemigo which the run() method can detect.

    Having said that, maybe Jammas was right: why does Enemigo implement Runnable? It doesn't actually do any lengthy work (that I can see). If all you want to do is not animate it for 100ms then the paint method could set some variable in Enemigo that marks it as "stopped" and also set a timer so that 100ms later the juego panel can mark it as "started" again. (How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features))

  7. #7
    Sebasnr is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: Problem 'sleeping' Threads

    Thanks pbrockway2, I'll check the swing Timers then.

Similar Threads

  1. static problem in threads
    By Mira1000 in forum New To Java
    Replies: 2
    Last Post: 11-16-2011, 03:59 AM
  2. Using threads will solve my problem?
    By George R. in forum Threads and Synchronization
    Replies: 8
    Last Post: 03-24-2011, 08:13 PM
  3. interrupt sleeping thread by mouse action
    By BigBear in forum AWT / Swing
    Replies: 5
    Last Post: 08-12-2010, 11:10 PM
  4. How to stop thread when it is sleeping
    By luoluo in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-24-2010, 04:09 AM
  5. problem with threads
    By amith in forum Threads and Synchronization
    Replies: 4
    Last Post: 07-10-2008, 08:46 AM

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
  •