#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Java Question: the best way to...


    Hi,

    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:

    Lets imagine I am programming a piece of software that plays a game like Pacman, where you have to avoid enemies, and there are things you have to collect.

    Although probably not the ideal way to code this, my main thread performs the action of running away from the enemies, and that is all (it doesn't collect the items you need, let's say it is food).

    I then have a thread which constantly runs and checks if any 'food' has been dropped in the game. Now what I want to do when a piece of 'food' has been dropped, is to immediately stop running away from the enemy and go to get the food (ignore the fact the path to the food may have an enemy on 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.

    Thanks.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    way to interrupt the main thread
    One way would be to use a variable whose value would tell the thread you want it to stop.
    tell it to ...
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0
    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.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    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.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0
    Originally Posted by NormR
    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
    To try and keep the theme of the Pacman game going:

    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.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    does the move
    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.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0
    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?
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    a more efficient way to check
    More efficient than an if statement? I don't think there are many statements simpler than an if statement, other than an assignment statement.

IMN logo majestic logo threadwatch logo seochat tools logo