Java线程未启动
我有一个IRC机器人,它托管游戏服务器,并提供一些参数。问题是,一旦它托管了一台服务器,它就会停止侦听IRC(实际上,一次只能托管一台服务器)。这不是我想要的 我以为线程将是我问题的答案,但我似乎无法让它工作。看起来它实际上不是从另一个线程开始的 下面是我的主要类,它通过线程启动并运行该方法:Java线程未启动,java,multithreading,Java,Multithreading,我有一个IRC机器人,它托管游戏服务器,并提供一些参数。问题是,一旦它托管了一台服务器,它就会停止侦听IRC(实际上,一次只能托管一台服务器)。这不是我想要的 我以为线程将是我问题的答案,但我似乎无法让它工作。看起来它实际上不是从另一个线程开始的 下面是我的主要类,它通过线程启动并运行该方法: // Everything is okay, run the server. Runnable r = new Server(this, channel); Thread thread = new Thr
// 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();
}
}
下面是(大概)控制线程的服务器类:
// 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();
}
}
我不是从一个线程开始的吗?谢谢你的帮助 恐怕不是
服务器类应该更像:
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;
}
}
恐怕不是
服务器类应该更像:
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;
}
}
您的
run()
方法为空;它开始、不执行任何操作,然后结束。您的run()
方法为空;它开始,什么也不做,然后结束。很抱歉这个愚蠢的问题,我真的应该多检查一下。谢谢你的回答!抱歉问了个愚蠢的问题,我真的应该多检查一下。谢谢你的回答!