Threading

Discussion in 'Plugin Development' started by MasterAsp, May 20, 2011.

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

    MasterAsp

    Hello, I'm creating a plugin that requires enormous amounts of block conversions and whenever I attempt to run it, simply put, the server doesn't like it at all. I've looked in to event scheduling and Minecraft's built in way of handling threads and such, but unfortunately I am in need of a asynchronous task but require the use of the Block.setTypeId method. Is there a way I may do this? Thank you.
     
  2. Offline

    garbagemule

    I'm not sure if the event scheduling you have looked into is the BukkitScheduler interface or not. If not, then I think this might be exactly what you're looking for. You could perhaps do the following in your block conversion method:
    Code:
    PluginNameThread thread = new PluginNameThread(block);
    plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin,thread);
    (Please clean this up a bit if you're doing more stuff with either the Server object or the BukkitScheduler :D)

    This should execute the run()-method in the PluginNameThread object as soon as possible. You simply feed the block to the method, and that should be all there is to it. I'm not big on anonymous classes (for aesthetic reasons), so I would make a class called (in this case) pluginNameThread, and have it look like this:
    Code:
    ...
    public class PluginNameThread implements Runnable
    {
        private Block block;
    
        public PluginNameThread(Block block)
        {
            this.block = block;
        }
    
        public void run()
        {
            block.setTypeId(0);
        }
        ...
    }
    (You'll notice the Allman-style indentation and braces arrangement for optimal readability! ;))

    I can't guarantee that this will work as I have not tested it myself, but I don't see off the bat why it shouldn't. If this is what you've been doing, can we perhaps see some code bits?

    Edit: Fixed interface implementation. Thanks Sammy :)
     
  3. Offline

    Sammy

    The class needs to extend Runnable...
    This way you are just creating a new thread for each block you want to set, I don't see that as a good solution, and doing blocksets in a Async thread is kinda a dangerous, if for some reason the same block is being changed on the mainthread you'll have cache problems (am I right?).
     
  4. Offline

    garbagemule

    ...implement* Runnable ;)

    Sounds about right. It will be a lot of threads, but I'm pretty sure it would do what the OP asks for. How safe it is, is a different matter - how about a lock?

    The memory usage depends on how the asynchronous method is defined. If it involves a thread-pool, no sweat. I guess you could use the synchronized version (scheduleSyncDelayedTask) with the same arguments to have the main thread take care of it, but that sort of defeats the purpose.

    You could swap the Block parameter in the constructor of the thread class with a Block[], and then change the body to a for-each loop. That way you can split the blocks into portions of size > 1, and avoid having too many threads running.
     
  5. Offline

    Sammy

    That would be my guess, but the safety isn't a different matter its the OP's problem.

    For what I understand he knows you too use the scheduler the problem is the safety of the asynchronous tasks, and that's a big issue.

    PS: the Synchronous one runs on the main thread so it wouldn't help
     
  6. Offline

    MasterAsp

    Thanks for your help, yes, that was my problem with the asynchronous task, the setBlockTypeId method, which isn't a safe method in asynchronous. I was thinking that I could do all the data part in the asynchronous loop then tell the main thread periodically to do block changes. Would that work?
     
Thread Status:
Not open for further replies.

Share This Page