2012-08-11 49 views
0

我有一個IRC機器人,它提供了幾個參數來託管遊戲服務器。問題是,一旦它託管了一臺服務器,它就會停止監聽IRC(這意味着實際上,一次只能託管一臺服務器)。這不是我想要的。Java線程不啓動

我認爲線程將是我的問題的答案,但我似乎無法得到它的工作。它似乎並不實際在另一個線程中啓動?

這裏是一個啓動並運行通過線程的方法我的主類:

// Everything is okay, run the server. 
Runnable r = new Server(this, channel); 
Thread thread = new Thread(r); 
thread.start(); 

這裏是服務器類(大概)控制線程:

public class Server extends PircBot implements Runnable { 

public void run() { 

} 

public Server (bot BotRun, String channel) { 
    String names[] = org.bestever.bebot.bot.hostbuilder.split(" "); 
    ProcessBuilder pb = new ProcessBuilder(names); 
    pb.redirectErrorStream(true); 
    try { 
     Process proc = pb.start(); 
     BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream())); 
     String strLine = null; 
     while((strLine = br.readLine()) != null) { 
      // Returns UDP Initialized if the server was successfully started 
      if (strLine.equalsIgnoreCase("UDP Initialized.")) { 
       BotRun.sendMessage(channel, "Server started successfully."); 
      } 
      // Returns Bad Hex Number if there is a problem with the WAD file 
      else if (strLine.startsWith("Bad hex number")) { 
       BotRun.sendMessage(channel, "Error starting server: "+strLine); 
      } 
      System.out.println(strLine); 
     } 
     Thread.currentThread().interrupt(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

,我沒有實際在線程中啓動它?謝謝你的幫助!

回答

3

恐怕不是。

服務器類應該更像:

public class Server extends PircBot implements Runnable { 

    private bot BotRun; 
    private String channel; 
    public void run() { 
     String names[] = org.bestever.bebot.bot.hostbuilder.split(" "); 
     ProcessBuilder pb = new ProcessBuilder(names); 
     pb.redirectErrorStream(true); 
     try { 
      Process proc = pb.start(); 
      Reader reader = new InputStreamReader(proc.getInputStream()); 
      BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream())); 
      String strLine = null; 
      while((strLine = br.readLine()) != null) { 
       // Returns UDP Initialized if the server was successfully started 
       if (strLine.equalsIgnoreCase("UDP Initialized.")) { 
        BotRun.sendMessage(channel, "Server started successfully."); 
       } 
       // Returns Bad Hex Number if there is a problem with the WAD file 
       else if (strLine.startsWith("Bad hex number")) { 
        BotRun.sendMessage(channel, "Error starting server: "+strLine); 
       } 
       System.out.println(strLine); 
      } 
      reader.close(); 
      Thread.currentThread().interrupt(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public Server (bot BotRun, String channel) { 
     this.BotRun = BotRun; 
     this.channel = channel; 
    } 
} 
2

run()方法是空的;它開始,什麼也不做,結束。

+0

對不起,這個問題真的應該檢查一下。感謝你的回答! – Ivan 2012-08-11 00:29:06