How to update a hashmap as soon as a thread is ended

Discussion in 'Plugin Development' started by BlueNova, Apr 18, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    BlueNova

    hello! im fairly new into java and having a problem with multi threading.

    I have a game called blockhunt which has the rules:
    -a player is given a random block to stand on in 5 mins
    -if a player doesnt reach that block in under 5 mins they lose
    -last one in a virtual game ( a thread) wins

    and currently i have my code written like so:
    -a hashmap containing every game(thread) with a generated ID as a key
    -a player list as an ArrayList<Player> under each game in the hashmap
    -before a game starts the class method isPlayerInGame checks if a player that was added to join a game is already in a game so that no player can be in 2 games at once

    i cant seem to get around this problem:
    -after a thread's code is done, i need to remove the game and the players from the hashmap
    so that they can join a new game if desired

    code:
    https://dpaste.org/4d9O#L

    also keep in mind the code i have doesnt apply to what i wrote about how the game works. its the game is still being built. thanks for any help in advance
     
  2. can't you just have a function that loops over the hashmap and removes it from the hashmap when it finds the gameroom that has to be closed, and then call that function at the end of the run method?
     
  3. Offline

    caderapee

    @BlueNova You cant use separate thread or sleep method like that with bukkit.

    You can totally manage many instances of game without the need of separate thread.

    Use scheduler instead
     
  4. Offline

    alant7_

    I guess you want to remove it from hashmap after the 'while (!noWinner)' loop is done, if yes, then just add GameMap.remove(GameID) below the try catch block. (in GameRoom).
     
Thread Status:
Not open for further replies.

Share This Page