Solved Error when reloading server (multithreading)

Discussion in 'Plugin Development' started by ThrustLP, Aug 3, 2016.

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

    ThrustLP

    Hey guys! So I have a class extending Thread:

    Code:
    package me.thruzzd.data;
    
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import me.thruzzd.ontime.Main;
    
    public class ReadThread extends Thread{
    
       
         public void run(){
             while (true){
                 ServerSocket sock = Main.getInstance().sock;
                 Socket client = null;
                 try {
                client = sock.accept();
                    
                     DataInputStream dis = new DataInputStream(client.getInputStream());
                         String data = dis.readUTF();
                        //TEST
                         System.out.println(data);
    
                     dis.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
            
         }
        
        
    }
    
    And I start that in my onEnable like:

    Code:
    Thread thread1 = new Thread(new ReadThread());
           
    thread1.start();
    So everything works fine, if I stop and start the server again there are no problems, but if I try to RELOAD it I always get the following error:

    Code:
    [23:00:42 WARN]: java.net.BindException: Address already in use: JVM_Bind
    [23:00:42 WARN]:        at java.net.DualStackPlainSocketImpl.bind0(Native Method)
    [23:00:42 WARN]:        at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
    [23:00:42 WARN]:        at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
    [23:00:42 WARN]:        at java.net.PlainSocketImpl.bind(Unknown Source)
    [23:00:42 WARN]:        at java.net.ServerSocket.bind(Unknown Source)
    [23:00:42 WARN]:        at java.net.ServerSocket.<init>(Unknown Source)
    [23:00:42 WARN]:        at java.net.ServerSocket.<init>(Unknown Source)
    [23:00:42 WARN]:        at me.thruzzd.ontime.Main.onEnable(Main.java:49)
    [23:00:42 WARN]:        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321)
    [23:00:42 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340)
    [23:00:42 WARN]:        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405)
    [23:00:42 WARN]:        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357)
    [23:00:42 WARN]:        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317)
    [23:00:42 WARN]:        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741)
    [23:00:42 WARN]:        at org.bukkit.Bukkit.reload(Bukkit.java:535)
    [23:00:42 WARN]:        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25)
    [23:00:42 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
    [23:00:42 WARN]:        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641)
    [23:00:42 WARN]:        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627)
    [23:00:42 WARN]:        at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412)
    [23:00:42 WARN]:        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375)
    [23:00:42 WARN]:        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654)
    [23:00:42 WARN]:        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557)
    [23:00:42 WARN]:        at java.lang.Thread.run(Unknown Source)
    [23:00:42 INFO]: [MafiOntime] Aktiviert
    [23:00:42 WARN]: Exception in thread "Thread-16"
    [23:00:42 WARN]: java.lang.NullPointerException
    [23:00:42 WARN]:        at me.thruzzd.data.ReadThread.run(ReadThread.java:18)
    [23:00:42 WARN]:        at java.lang.Thread.run(Unknown Source)
    How can I get rid of it?

    Thank you guys!

    - Tobi
     
  2. Offline

    Zombie_Striker

    @ThrustLP
    That is because even though you are reloading the server, you are not stopping the thread. You need to stop the thread before reloading.

    What you are getting is an NPE. You are getting it because you are using an object that is linked to the main class. Every time the server reloads, all main classes are deleted, so you are trying to access an object that has been deleted.
     
  3. Offline

    ThrustLP

    @Zombie_Striker And how would I do that? I tired putting thread1.stop(); in my onDisable
     
  4. Offline

    ArsenArsen

    replace the true with isInterrupted() then use interrupt()
     
  5. Offline

    Zombie_Striker

    @ArsenArsen @ThrustLP
    Make sure that the onDisabled does not complete unless the thread has been stopped. Create a while loop until something says the thread has been stopped.
     
  6. Offline

    ArsenArsen

  7. Offline

    ThrustLP

    @ArsenArsen @Zombie_Striker Hmm I cannot manage to do that...

    My onDisable now looks like:

    Code:
    System.out.println("Stoppt Thread...");
            thread1.interrupt();
           
           
            while(thread1.isAlive()){
                //Do I need to put sth in here?
            }
    and I changed the while(true) to while(!Thread.currentThread().isInterrupted())

    But I still get the same errors.
     
  8. Offline

    I Al Istannen

    @ArsenArsen @ThrustLP @Zombie_Striker
    Or a common semaphore. (Probably not quite suited for this, but probably better than the atomic boolean)
    Or a while loop with "Thread#isAlive" as the condition.
    Or call Thread#join to wait for the other thread to die.


    What errors do you get? Is the thread stopped? Does the while loop ever exit? Make a println below it.

    What is your new code?

    Is the thread started after Main.getIstance() is assigned?
     
  9. Offline

    ThrustLP

    @I Al Istannen I get the same errors I mentioned in my first post, If I put a println in the while it runs on and on and on forever, the thread is started before assigning it, my new code is:

    Code:
    package me.thruzzd.data;
    
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import me.thruzzd.ontime.Main;
    
    public class ReadThread extends Thread{
    
       
         public void run(){
             while (!Thread.currentThread().isInterrupted()){
                 ServerSocket sock = Main.getInstance().sock;
                 Socket client = null;
                 try {
                client = sock.accept();
                    
                     DataInputStream dis = new DataInputStream(client.getInputStream());
                         String data = dis.readUTF();
                        //TEST
                         System.out.println(data);
    
                     dis.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
            
         }
        
        
    }
    

    Code:
    System.out.println("Stoppt Thread...");
            thread1.interrupt();
            while(thread1.isAlive()){
                System.out.println("WHILE");
            }
     
  10. Offline

    I Al Istannen

    @ThrustLP
    You must assing the new instance before starting the task.

    And I think the ServerSocket#accept method doesn't care for the interrupt and just waits on. In this case you need this. Just do it in your onDisable and your thread should kill itself by throwing the exception. Then catch the socketException in the catch block you already have and break the while loop if that happens.
     
  11. Offline

    ThrustLP

    @I Al Istannen Where would I have to close the serversocket and how :/

    Solved it thank you all!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 3, 2016
    I Al Istannen likes this.
Thread Status:
Not open for further replies.

Share This Page