September 8th, 2013, 07:29 PM
Java Question: the best way to...
I would like some opinions on the best way to go about doing something. I will try and explain as best as I can what I want to achieve, and hopefully you can advise me on what options there are.
This scenario isn't my project, but I thought this would be the best way to explain it:
What is the best way to interrupt the main thread and tell it to use the method to get the food, rather than to avoid the enemies.
Hopefully I've given a clear enough example to get a bit of help on this.
September 8th, 2013, 08:15 PM
One way would be to use a variable whose value would tell the thread you want it to stop.
Put a value in a variable that says what you want it to do.
Last edited by NormR; September 8th, 2013 at 08:18 PM.
September 8th, 2013, 08:37 PM
Are you meaning to have something such as a boolean, and if it's set to true, perform the new action?
If so, the reason I don't want to do this is because it requires the main thread to constantly check this boolean, where really I want to be to interrupted and told to do the new action.
If I misunderstood you please say.
September 8th, 2013, 09:26 PM
Where could/would the code in the thread be executing when it was interrupted?
Could it be in the middle of some multi-step task that needed to be completed before it is safe to interrupt the thread?
How would the execution of the thread get from anywhere in the code it was executing to the place where it could do the new action?
It would be better if the thread tested a value and stopped executing at a safe place in the thread's execution instead of stopping it anywhere.
It's explained here: http://docs.oracle.com/javase/7/docs...precation.html
Last edited by NormR; September 9th, 2013 at 09:04 AM.
September 9th, 2013, 09:08 AM
To try and keep the theme of the Pacman game going:
Originally Posted by NormR
The main method, in one statement, plans the next move and subsequently does the move (not moving 1 space, but more, so it could be moving from one corner to another).
However, with food, we don't want it to go all the way to the other side and then get the food, I want it to stop this move at whatever stage it's at, and then start to get the food.
September 9th, 2013, 09:24 AM
The code that does the move must be ready to stop moving when it needs to go get the food.
Perhaps something like this:
test if the moving needs to stop
if so, return from the move method with a value saying why the move method returned.
Normal return value would be: Move_Completed.
Another value could be: Stopped_For_Food
Last edited by NormR; September 9th, 2013 at 09:26 AM.
September 9th, 2013, 09:26 AM
I can see the way I've coded my method needs some alteration now. Is there a more efficient way to check if it needs to move that simply using a variable (such as a boolean) which uses an if statement to check each time in a while loop?
September 9th, 2013, 09:29 AM
More efficient than an if statement? I don't think there are many statements simpler than an if statement, other than an assignment statement.