Scheduler help, server crashing

Discussion in 'Plugin Help/Development/Requests' started by CaptainDirt, Dec 26, 2014.

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

    CaptainDirt

    I haven't really gone into Bukkit Schedulers because I'm too lazy so normally I just use the following code as timers:
    Code:
            public void run()
                {
                    while (true)
                        {
                            try
                                {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e)
                                {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            counter--;
                            if (counter == 30)
                                {
    //Example
                                }
                        }
                }
    But I need to switch to using Spigot which now just gives me errors, so I decided to change to runnables but now when I teleport players at the end of the thread the server times out and crashes. I've heard you shouldn't do teleporting outside of the thread, but a lot of my code is in one class and is all pretty much connected to everything else in the class. Any help would be very excellent.
     
  2. Offline

    adam753

    There's no need to do a while(true) loop inside the scheduler's method. The point of the scheduler is that it handles the timing for you; you're treating it as a thread, which it isn't. Look up any tutorial on BukkitRunnables to help with using them, but basically you just need to remove the Thread.sleep part.
     
  3. Offline

    CaptainDirt

    That's what I used to do, now I do this:
    Code:
            public void callS()
                {
                    i = Bukkit.getServer().getScheduler()
                            .scheduleSyncRepeatingTask(plugin, new Runnable()
                                {
                                    public void run()
                                        {
                                            counter--;
    
                                            if (counter == 30)
                                                {
                                                    //Example
                                                }
                                        }
                                }
                }
     
  4. Offline

    mythbusterma

    That sounds pretty.....no.

    "Connectedness" has nothing to do with thread safety, I don't care how "connected" your class is, don't violate the thread safety of the server due to ignorance.

    Anyway, it would certainly help if you posted the entire class and the error, then explain the desired and resultant behaviour.
     
  5. Offline

    teej107

    So you made sure that your threads are all in sync? Sounds like a lot of work for a simple task.
     
  6. Offline

    CaptainDirt

    There is no error. It just straight up crashes. (This is what I'm trying to fix) And the code is pretty much just getting cords out of a config, turning them into a location then teleporting. (The location is always in a diff world)

    As far as I'm aware yes.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  7. Offline

    teej107

    @CaptainDirt So you are using synchronized methods, thread locking and/or volatile fields, so no concurrency errors occur?

    It doesn't matter where the methods are or if they are in the same class. Different threads can access the same thing in classes.
     
  8. Offline

    mythbusterma

    @teej107

    Volatile fields make no difference in this case, none of the Bukkit API is thread safe, and therefore cannot be accessed from another thread, not matter how many times you use the synchronized keyword.


    @CaptainDirt

    If you used the word "async" anywhere in your code, I can 100% guarantee that you did not "synchronize" your threads. Just don't do it. It's really not that hard.
     
    teej107 likes this.
  9. Offline

    CaptainDirt

    Never used it. All threads scheduled a Sync task.
     
  10. Offline

    mythbusterma


    You could see why I misunderstood. Anyway, are you still having issues?
     
  11. Offline

    CaptainDirt

    Yes, whenever I go to teleport them it obviously breaks the plugin and the server crashes, tell me how I can fix this please :D
     
  12. Offline

    mythbusterma

    @CaptainDirt

    Post your entire class, and post the crash log of the server.
     
  13. Offline

    CaptainDirt

    My class is too big to post, but this is what happens: I have an event set to trigger a runnable when there is x number of players online, the runnable is a sync task (this is in a single class) and when the "counter" reaches 0 I access my big class to teleport the players to a position (which crashes the server) And there is no log, it just laggs / times out.
     
  14. Offline

    mythbusterma

    @CaptainDirt

    Then it's probably too large. Spread it out. There is a crash log if the server crashes.
     
  15. Offline

    CaptainDirt

    Okay, I'll try that. And there isn't a crash log :/ it just times out and I need to force close it.

    Didn't work. Anyway, I've reverted my code to this:
    Code:
        public void run()
                {
                            try
                                {
                                    Thread.sleep(1000L);
                                } catch (InterruptedException e)
                                {
                                    e.printStackTrace();
                                }
                            counter--;
                            if (counter == 30)
                                {
                                }
                }
    And when I want to teleport a player this shows up:
    http://hastebin.com/feyekonuyo.avrasm
    The plugin works fine on Bukkit but when I use Spigot, all hell is raised... any ideas? (Also this is what the original error was but when I looked into it I saw that it was some scheduler problem...but it doesn't really look like that)

    EDIT: I just did a little more research and someone said on another thread the thread it "async" and that you should access the API in those threads, but when I use a Bukkit Scheduler it gives me grief and lags/crashes/timesout the server with no logs/stack traces.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  16. Offline

    mythbusterma

    @CaptainDirt

    Again, why in the world are you sleeping the server? We said don't do that, and you constantly refuse to post your code so we can't help you.

    It's very simple why it's doing that, you're sleeping the server when you shouldn't be.
     
  17. Offline

    CaptainDirt

    That's not it lol...this thread code is in it's own single class. I call the class, it does the countdown but then shows an error.

    (What I edited into my last post:
    )
     
  18. Offline

    mythbusterma

    @CaptainDirt

    That post makes absolutely no sense at all. What is "this thread code?" What is that class? Post your entire class.
     
  19. Offline

    CaptainDirt

    I've just cut out the messages that are broadcasted and player.teleport(loc);
     
  20. Offline

    mythbusterma

    @CaptainDirt

    So you've posted a method. That's all you've posted. I said

    And in that context "class" means your entire main class.
     
  21. Offline

    CaptainDirt

    Now you've just confused me, why do you want my main class?
     
  22. Offline

    mythbusterma

    @CaptainDirt

    To see what you're doing to cause the client to time out.
     
Thread Status:
Not open for further replies.

Share This Page