Scheduler Help

Discussion in 'Plugin Development' started by LastTalon, Jul 26, 2012.

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


    Hello, I was just wondering what the advantages would be of using the scheduler vs just starting my own thread. Any help will be appreciated.
  2. Offline


    In short: Thread safety. You'll need that to do just about anything with server and in-game objects.

    If you had a thread for doing DB access, cleaning out some sort of LRU cache, calculating the next step in a cellular automa system you built, etc then those are just fine and dandy. But don't touch any block object, server object, character, item stack, or nearly anything at all from the whole of the org.bukkit package and its children.

    The next thought to have, then, is how to minimise the work done in each scheduler call, because everything else is waiting on your code to complete. That, however, is a whole different can-o-worms
  3. Offline


    Well on the wiki page it says not to use the bukkit api anyway because it would cause the main thread to sleep, which makes sense. So I was just wondering why, if I can't access bukkit anyway why I would use the scheduler.
  4. Offline


    I think this is the page you are referring to?

    If I'm reading this right, and I like to think that I am (due to experience that reinforce this view), then it is telling you not to sleep lest you lock the whole of the server. Like I said:
    Also that wiki page is also telling you to <reallyImportant> ONLY <reallyImportant> access bukkit API calls from code running via the scheduler.

    Could you point to the text that is leading you to different conclusions?
  5. Offline


    I think it may have to do with the lack of distinction between a normal thread and a thread made by the scheduler. So you're saying its ok if I make an asynchronous task through the scheduler that uses API calls as long as I'm sure that resources aren't being used at the same time correct?
  6. Offline


    Yes. It certainly is. In fact that is the only safe way to do that.

    Correct kinda. It isn't your job to make sure that Bukkit resources are ready for you, that is Bukkit's job. That is why the scheduler exists in the first place. It will only call your task when all other Bukkit/server tasks are quiescent. Your task is the only one doing anything.

    The difference between a normal thread and the scheduler is that the scheduler is running in THE main server thread that does absolutely everything server-like. It issues events, saves chunks, spawns monsters, tracks players, pawns blocks, and everything else the server has to do. When it comes time to run your task it is putting all that off till you are done. This lets you manipulate the server's internals (and, therefore, the MC world) safely. Nothing else should be touching it.

    This is a basic thread safety model of the sort seen for at least as long as I've been programming, but the first time you see it (especially in Java where you get a lot of freedom and there are a lot of thread safe things right out of the gate) this model can be a bit unintuitive.

    Make more sense now?
  7. Offline


    Well in a normal java thread there's nothing stopping you from manipulating the same resource from two different threads at once, which is usually a bad idea. Thats why I was wondering.
Thread Status:
Not open for further replies.

Share This Page