Async Chat & Thread Safety

Discussion in 'Plugin Development' started by chaseoes, Dec 7, 2012.

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

    chaseoes

    I brought up this topic ages ago, but I have yet to come to a conclusion on what should be done.

    To keep my chat plugin thread safe, I've started storing player's prefixes and suffixes in a hashmap. When they log in I add their prefix to it, remove it when they log out. But this presents an issue if their group changes - the plugin is still using the "cached" version if their group until they log out and back in again.

    I thought about making a repeating task that re-caches their prefix etc., every minute or so, but this can still present a problem. What if it runs at the same time as when they chat?

    Has anyone come up with a good solution with how to keep chat plugins thread safe?
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    A standard HashMap is not thread safe. i.e. it is not safe for one thread to put things into the map while another thread reads from it.
     
  3. Offline

    jayfella

    A vector should suffice and is threadsafe, just create a class to store the properties instead of a key/value pair.
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    A vector is essentially a type of list. You loose the ability to quickly determine a value for a given player with a list. Maps and Lists are completely different data structures with completely different uses.
     
  5. Offline

    jayfella

    An iteration over 200 players is barely even arguable. Its the equivalent of using a StringBuilder for
    Code:
    String welcome = "hello " + player.getName() + " and welcome!".
     
  6. Offline

    chaseoes

    So... wat do?
     
  7. Offline

    jayfella

    I dont actually understand the problem. If you pass the prefix properly, it should be passed by reference, not given a cloned copy. If you change the property, everything else that has a reference to it should change with it - because it is given by reference - that is to say that the program said "what you are looking for is 'here'" - and not "here's a new copy of what you want".

    Not really a good example. Let me explain further. I have one spanner and three mechanics. Whenever a mechanic needs the spanner, i say "its over there" - i pass the spanner by reference to its location. If i give him a new spanner, i have "cloned" the object - and so any change to the first spanner is not reflected on the new one. Im terrible at explaining things. I hope that makes sense.


    A typical example of it being passed as a cloned object is using a foreach loop instead of a for loop. So the problem isn't really "how" to do it, it is "why" your property isn't being updated when it has changed.
     
    JazzaG likes this.
  8. Offline

    Sagacious_Zed Bukkit Docs

    to extend the metafore. What happens when you say the spanner is over there, and a third person moves the spanner, it is no longer where you said it will be. If you actually give him a new spanner, it does not matter where the old one is.
     
  9. Offline

    chaseoes

    I have absolutely no clue what you just said. First of all, I would normally get the prefix like so:
    Second, what is a spanner!?
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    chaseoes This is a spanner

    And that method of getting the prefix is not thread-safe. Because it suffers from the problem i explained earlier with the spanner example.
     
  11. Offline

    chaseoes

    Yes.. and the point behind this thread was me looking for an alternative way since that's not thread safe.. you're just re-enforcing what I stated in the first post. :p
     
  12. Offline

    Sagacious_Zed Bukkit Docs

    At the most basic level you should have come across this is your search. http://stackoverflow.com/questions/1792023/thread-safe-map-for-java
    However since anything key-value pair placed in the thread safe map would be not updated with the configuration, you would need to find a way to mirror the data. Ill leave this as a further exercise.

    Show Spoiler
    This is my plugin that does local chat. https://github.com/SagaciousZed/SampleLocalChat
     
  13. Offline

    chaseoes

    What..? You just bumped a post from over a year ago and contributed nothing to the conversation.
     
Thread Status:
Not open for further replies.

Share This Page