【发布时间】:2016-04-30 05:53:47
【问题描述】:
我有一个方法chatHandler();,其中包含相当多的代码。它还与程序的其余部分一样在单独的线程上运行。
在main 方法中,我有一个while 循环,只要running == true 像这样运行它:
while (running) {
chatHandler();
}
(问题 1)我的问题是,由于我在不同的线程上运行 chatHandler();... 调用该方法的速度是否可以比其中的代码运行得更快?我的程序的结果似乎告诉我情况就是这样。
这对 Google 来说似乎也太具体了,就在你烤我之前。因此,如果问题 1 是肯定的,那么我如何确保 while 函数的循环速度与 chatHandler(); 可以处理的一样快?
这是我的chatHandler(); 方法中的代码:
public static void chatHandler() {
new Thread(new Runnable() {
public void run() {
LocalDateTime now = LocalDateTime.now();
int hour = now.get(ChronoField.HOUR_OF_DAY);
int minute = now.get(ChronoField.MINUTE_OF_HOUR);
int second = now.get(ChronoField.SECOND_OF_MINUTE);
String hourSyntax = Integer.toString(hour);
String minuteSyntax = Integer.toString(minute);
String secondSyntax = Integer.toString(second);
if (hour < 10) {
hourSyntax = "0" + hourSyntax;
}
if (minute < 10) {
minuteSyntax = "0" + minuteSyntax;
}
if (second < 10) {
secondSyntax = "0" + secondSyntax;
}
String chatChecker = "[" + hourSyntax + ":" + minuteSyntax + ":" + secondSyntax +
"] [Server thread/INFO]: <";
//System.out.println(chatChecker);
outputPretext = outputLine.substring(0, 34);
if (outputPretext.equals(chatChecker)) {
userNameArray = outputLine.split("~");
preUserName = userNameArray[1];
userNameArray2 = preUserName.split(">");
userName = userNameArray2[0];
teamCheck = outputLine.substring(34, 36);
playerMessageArray = outputLine.split(">");
playerMessage = playerMessageArray[1].substring(1);
/**
twitchRank = "§T§r§~";
youTubeRank = "§Y§r§~";
owner = "§O§r§~";
admin = "§A§r§~";
spookRating1 = "§1§r§~";
spookRating2 = "§2§r§~";
member = "§M§r§~";
*/
//chatCurseReplace();
//chatSpamCheck();
//mutedPlayer();
switch (teamCheck) {
case "�T":
try {
writer.append("/tellraw @a [\"\",{\"text\":\"* \"},{\"text\":\"Twitch \",\"bold\":true,\"color\":\"dark_purple\"},{\"text\":\"" + userName + "\",\"color\":\"blue\"},{\"text\":\" » " + playerMessage + "\"}]");
writer.newLine();
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
break;
case "�Y":
try {
writer.append("/tellraw @a [\"\",{\"text\":\"* You\"},{\"text\":\"Tube \",\"bold\":true,\"color\":\"red\"},{\"text\":\"" + userName + "\",\"bold\":true,\"color\":\"blue\"},{\"text\":\" \",\"bold\":true},{\"text\":\" » " + playerMessage + "\"}]");
writer.newLine();
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
break;
case "�O":
try {
writer.append("/tellraw @a [\"\",{\"text\":\"* \"},{\"text\":\"Owner \",\"bold\":true,\"color\":\"dark_red\"},{\"text\":\"" + userName + "\",\"color\":\"red\"},{\"text\":\" > " + playerMessage + "\"}]");
writer.newLine();
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
break;
case "�A":
try {
writer.append("/tellraw @a [\"\",{\"text\":\"* \"},{\"text\":\"Admin \",\"bold\":true,\"color\":\"dark_red\"},{\"text\":\"" + userName + "\",\"color\":\"red\"},{\"text\":\" » " + playerMessage + "\"}]");
writer.newLine();
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
break;
case "�1":
try {
writer.append("/tellraw @a [\"\",{\"text\":\"* \"},{\"text\":\"[\",\"color\":\"dark_green\"},{\"text\":\"S\",\"color\":\"green\"},{\"text\":\"]\",\"color\":\"dark_green\"},{\"text\":\" \"},{\"text\":\"" + userName + "\",\"color\":\"blue\"},{\"text\":\" » " + playerMessage + "\"}]");
writer.newLine();
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
break;
case "�2":
try {
writer.append("/tellraw @a [\"\",{\"text\":\"* \"},{\"text\":\"[\",\"color\":\"dark_green\"},{\"text\":\"Spook\",\"color\":\"green\"},{\"text\":\"]\",\"color\":\"dark_green\"},{\"text\":\" \"},{\"text\":\"" + userName + "\",\"color\":\"blue\"},{\"text\":\" » " + playerMessage + "\"}]");
writer.newLine();
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
break;
case "�M":
try {
writer.append("/tellraw @a [\"\",{\"text\":\"* \"},{\"text\":\"" + userName + "\",\"color\":\"dark_gray\"},{\"text\":\" » \"},{\"text\":\"" + playerMessage + "\",\"color\":\"gray\"}]");
writer.newLine();
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
break;
case "NO":
break;
default:
try {
writer.append("say Chat Error: no team");
writer.newLine();
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
outputLine = "****************************************************************";
}
}
}).start();
}
就是这样,谢谢!
【问题讨论】:
-
我需要更多上下文来更好地解释;但是,根据您的代码,更好的方法是始终在后台运行一个线程(处理聊天消息),而不是每次都启动一个新线程......(看起来你正在做,再次断章取义。)
-
你为什么首先使用线程? (是的,除非您添加特定的同步原语,否则无法保证您的代码运行的顺序。)
-
看起来您将启动数千个线程。每次调用
chatHandler()都会启动一个新线程并立即返回。你应该很快地填满内存,更不用说让线程调度器在同时运行这么多线程时头疼。 -
@Kaelinator 似乎您想确保仅在上一次调用完成后才调用
chatHandler方法。如果是这样,您可以在与while循环相同的线程中完成这项工作,而不是产生一个新线程。
标签: java multithreading loops methods while-loop