Java线程无法启动

时间:2012-08-11 00:17:03

标签: java multithreading

我有一个IRC机器人,它提供了一些参数来托管游戏服务器。问题是,一旦它托管服务器,它就会停止监听IRC(实际上,一次只能托管一台服务器)。这不是我想要的。

我认为线程可以解决我的问题,但我似乎无法让它工作。看来它实际上并没有在另一个线程中启动?

这是我的主要类,它通过线程启动并运行方法:

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

这是Server类(可能)控制线程:

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();
    }
}

我是不是真的在线程中启动过它?谢谢你的帮助!

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:2)

您的run()方法为空;它开始,什么都不做,结束。