Ivan
Ivan

Reputation: 87

Java thread not starting

I have an IRC bot which hosts game servers given a few arguments. The problem is, once it hosts a server, it stops listening to IRC (meaning realistically, only one server can be hosted at a time). This is not what I want.

I assumed threading would be the answer to my problem, but I can't seem to get it to work. It appears that it doesn't actually start in another thread?

Here is my main class which starts and runs the method via threading:

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

And here is the Server class which (presumably) controls the threading:

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

Have I not actually started it in a thread? Thanks for any help!

Upvotes: 0

Views: 405

Answers (2)

WhyNotHugo
WhyNotHugo

Reputation: 9924

Your run() method is empty; it starts, does nothing, and ends.

Upvotes: 2

Abhinav
Abhinav

Reputation: 2085

I am afraid, not.

Server class should be more like:

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

Upvotes: 3

Related Questions