Thread safety - Calling a async event

Discussion in 'Plugin Development' started by libraryaddict, Jan 9, 2013.

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

    libraryaddict

    Inside a AsyncPlayerChatEvent I want to call a event. However I'm fairly sure that Bukkit.getPluginManager().callEvent(event) is not thread safe.

    How should I be calling the event? Preferably I could do this from the thread and not by calling it from the main class with a scheduler.

    AsyncPlayerChatEvent is called from a thread. So how would I do this?
     
  2. Offline

    skore87

    If you're uncertain if it is thread safe with that method, you could have the event call to a synchronized method in it's own class (main thread) to do it.
     
  3. Offline

    libraryaddict

    I know its not thread safe.

    I'm enquiring if there is a way to call it from a thread as cb does.
     
  4. You can use the scheduler (whose methods themselves are thread-safe) to run a sync task that will be executed on the next tick of the main thread.
    If you need to process results from the event, you can also use "callSyncMethod", which returns you a java Future object to wait for results.
    More information: http://wiki.bukkit.org/Scheduler_Programming#Tasks_which_return_a_value
     
  5. Offline

    md_5

    callEvent is thread safe, if the event being called is an async event.
     
    libraryaddict likes this.
  6. Offline

    libraryaddict

    md_5

    Turns out I also need to send the player the message. Is player.sendMessage(message) thread safe?
     
  7. Offline

    md_5

    Yes.
     
    libraryaddict likes this.
Thread Status:
Not open for further replies.

Share This Page