Concurrency Question

Discussion in 'Plugin Development' started by meguy26, Jun 20, 2015.

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

    meguy26

    So I have recently started a project which will start multiple threads that all send commands to one central command thread. Now, my questions is, would it be better to just execute the commands from the command thread, or start an every-tick repeating task that takes the commands from the command thread and runs them on the main thread?
     
  2. Offline

    teej107

    Most code processes happen so fast that you don't need an extra thread to do the work. Bukkit API methods should always be called from the main thread.
     
    Konato_K likes this.
  3. Offline

    meguy26

    @teej107
    So i should use the scheduler? (the threads are there for a reason)
     
  4. Offline

    mythbusterma

    @meguy26

    They're there for work that needs to be done outside the main thread. What kind of work exactly are you trying to do? A good rule of thumb in this industry is to not optimise until it presents a clear problem.
     
    meguy26 likes this.
  5. Offline

    meguy26

    @mythbusterma
    OK, I'll go into detail:

    Although it already exists, I am trying to create a Remote Console, basically the same as http://dev.bukkit.org/bukkit-plugins/cloudconsole/. I know it seems pointless, but I want to test my java skills and learn some networking while I am at it. The threads on the plugin are for listening and interacting with clients. These thread will funnel commands through a LinkedBlockingQueue. The question I am asking is should I use a scheduler that runs every tick to execute these commands, or do it from an alternate thread.
     
  6. Offline

    blablubbabc

    If those commands involve calls to the bukkit api then yes, schedule a synchronous task, either repeating, which checks if there are new commands in the queue, or you can schedule a 1-time (1-tick delayed) task from within your separate thread (the one which receives the commands from the clients) whenever a new command arrives.
     
  7. Offline

    mythbusterma

    @meguy26

    Use the scheduler to add a repeating task to the main thread that executes the commands. Have a seperate thread in the background listening for new commands to come in.
     
Thread Status:
Not open for further replies.

Share This Page