Thread safety

Discussion in 'Plugin Development' started by wirher, Aug 4, 2012.

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

    wirher

    Hello.
    I'm writing plugin that will use one thread that collects data about players and put it into hashmap and another thread would verify this data. But how can I grant access to this hashmap and keep it thread safe?
     
  2. Offline

    Taco

    How I've seen this done, is that you do whatever you're doing only a set amount (or less) per tick, to avoid having your thread fall out of sync. To allow access to your class holding the HashMap, you could simply make your class that extends Thread require an instance of the data holding class when being constructed. An example of what I mean about that:

    Code:
    class myThread extends Thread{
     
    DataClass data;
     
    public myThread(DataClass instance){
    data=instance;
    }
     
    }
     
  3. Offline

    Njol

    Use the synchronized keyword:
    Code:
    synchronized(myHashMap) {
      // you can do whatever you want with the map here
    }
    Just remember that you have to put synchronized(myHashMap) {...} around every single code block that uses the map.
     
  4. Offline

    wirher

    Okay, I see now. Thank you.
    It is true that multi-tasking in Java is very easy.
     
  5. No it's not. It might be easier than in other languages, but it's still quite complex to completely understand. It looks simple at first glance but synchronized doesn't solve anything if not used properly.

    If you have to work with multi-threading in your plugin, I suggest you read this tutorial about concurrency. It explains the deal a bit more in depth than just "put synchronized and you're good", because that's not the case most of the time and can lead to issues that one might not notice if used incorrectly.

    In this case, you could also wrap your map with Collections.synchronizedMap(...), however, that won't help if you iterate over the map! Then you still need to manually synchronize the whole iteration process. If all you need to do on the other thread is iteration, you might as well not do the wrapper, since it won't be much help then.
     
  6. Offline

    Cirno

    Threading is not easy. The basic start() is the easy part. The hard part is stop().
    Think of threading this way.
    There is an unstoppable force (threading) that you're trying to stop without erroring.
     
  7. Offline

    wirher

    As Im not sure what to thing about threading now I'll explaing my situation.
    Thread 1: Collects data about players and puts data in hashmap. ( event - > hashmap.put(foo) )
    Thread 2: Checks all data from hashmap if it is valid. ( iterate, compares data available in hashmap and if not valid deletes key:value)
    Thread 3: Takes data from hashmap and reacts to it. ( iterate, only reading )

    Thread 3: could work on copy of that hashmap, so it won't affect general performance.
    Thread 2: as I can't delete keys directly from loop I could store "bad" keys in array, select them from copied hashmap and finally execute few changes on real one.

    Will it work? As I see it would use less cpu, more memory.
     
Thread Status:
Not open for further replies.

Share This Page