Results 1 to 7 of 7
Thread: Problem 'sleeping' Threads
- 12-31-2011, 02:27 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 8
- Rep Power
- 0
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 understandableLast edited by Sebasnr; 12-31-2011 at 02:30 AM.
- 12-31-2011, 03:09 AM #2
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
Re: Problem 'sleeping' Threads
Do you have more than one thread?
If so, WHY?
- 12-31-2011, 03:30 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 12-31-2011, 03:34 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 12-31-2011, 04:19 AM #5
Member
- Join Date
- Dec 2011
- Posts
- 8
- Rep Power
- 0
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,,,
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"
- 12-31-2011, 07:03 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Problem 'sleeping' Threads
Thread.sleep() will sleep the currently executing thread (the one doing the painting) so that's no good.Creating a new Thread(enem[i]) and then calling Thread.sleep(100) when the spaceship crash with an enemy
But if you call setSprite() as a result of "if(validarColision(nave, enem[i]))" then the currently executing thread is also the event queue :(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"
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))
- 12-31-2011, 05:48 PM #7
Member
- Join Date
- Dec 2011
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
static problem in threads
By Mira1000 in forum New To JavaReplies: 2Last Post: 11-16-2011, 02:59 AM -
Using threads will solve my problem?
By George R. in forum Threads and SynchronizationReplies: 8Last Post: 03-24-2011, 07:13 PM -
interrupt sleeping thread by mouse action
By BigBear in forum AWT / SwingReplies: 5Last Post: 08-12-2010, 10:10 PM -
How to stop thread when it is sleeping
By luoluo in forum Threads and SynchronizationReplies: 2Last Post: 04-24-2010, 03:09 AM -
problem with threads
By amith in forum Threads and SynchronizationReplies: 4Last Post: 07-10-2008, 07:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks