【发布时间】:2024-01-17 09:37:01
【问题描述】:
请帮我弄清楚为什么 Mac OS X Java 的运行时间是 Windows XP Java 的 5 倍。
我有一些代码在 Mac 和 PC Java 上表现不同。我有一个 Java GUI,可以与 Windows XP 机器上的多个“服务器”通信。
当我在另一台 Windows XP 机器或 linux 机器上运行 GUI 时,LabView 会收到消息并在 1 秒内做出响应。
当它从 Mac OS X 机器上运行时,需要 5 秒。暂停似乎是(从我们可以看出的调试中)我认为我发送字符串“pot 7\r\n”和 LabView 实际接收到它的时间之间。
当来自 Windows 时,LabView 会立即看到 pot 7 命令(我们有一个显示器要检查),但根据 LabView 程序员的说法,当从 Mac OS 机器发送时,该命令直到 5 秒过去后才会显示在屏幕上.
我会尽量在这里提供足够多的代码,让知道更多的人可能会说啊哈!
String IPaddress;
int commPort;
BufferedReader reader;
PrintWriter writer;
Socket sock;
long timeout = 8000;
...
public synchronized void connectCommPort() {
if (commportconnected != true) {
try {
sock = new Socket();
InetSocketAddress endpoint = new InetSocketAddress(IPaddress, commPort);
sock.connect(endpoint, timeout);
sock.setSoTimeout( timeout );
reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
writer = new PrintWriter(sock.getOutputStream());
commportconnected = true;
errorstatus = false;
} catch (IOException ex) {
logwriter("LabV: WARNING - network connection to Labview command port failed."+ex.toString());
commportconnected = false;
errorstatus = true;
}
}
}
...
public synchronized float[] readpots() {
String message = "pot 7";
connectCommPort();
if (commportconnected) {
try {
writer.print(message + "\r\n");
writer.flush();
logwriter("LabV: [sent] " + message);
shortpotvalues = potslistener();
} catch (Exception ex) {
}
disconnectCommPort();
}
potvalues[0] = shortpotvalues[0];
potvalues[1] = shortpotvalues[1];
potvalues[2] = shortpotvalues[2];
potvalues[3] = shortpotvalues[3];
potvalues[4] = shortpotvalues[4];
potvalues[5] = shortpotvalues[5];
potvalues[6] = shortpotvalues[6];
potvalues[7] = 5.0f;
return potvalues;
}
public synchronized float[] potslistener() {
String message = null;
int i = 0;
try {
//while ((message = reader.readLine()) != null && i < shortpotvalues.length) {
while (i < shortpotvalues.length) {
message = reader.readLine();
if ( message != null )
{
logwriter("LabV: [received] " + message);
if (message.contains("."))
{
shortpotvalues[i] = Float.parseFloat(message);
i++;
}
}
else
{
logwriter("LabV: received NULL unexpectedly, may not have all pots correct");
}
} // close reader-ready-while
} catch (ArrayIndexOutOfBoundsException aiofbex) {
logwriter("LabV: in potslistener() Array out of bounds! " + aiofbex.toString());
} catch (Exception ex) {
logwriter("LabV: in potslistener() got exception: " + ex.toString());
}
return shortpotvalues;
}
在 Mac OS X 10.8 上。运行 java -version 给出:
marks-Mac-mini:~ mark$ java -version
java version "1.6.0_43"
Java(TM) SE Runtime Environment (build 1.6.0_43-b01-447-11M4203)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01-447, mixed mode)
在 Windows XP 上(当 GUI 在 windows 上运行并且工作正常时使用的 java)给出:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\gus>java -version
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) Client VM (build 23.3-b01, mixed mode, sharing)
最后这里是来自 PC 的部分日志:
2013-03-19T11:45:22.000 LabV: [sent] pot 7
2013-03-19T11:45:22.921 LabV: [received] 2.310835
2013-03-19T11:45:22.921 LabV: [received] 2.447397
相应地来自 Mac(注意发送和第一次接收之间的 5 秒):
2013-03-13T12:13:17.092 LabV: [sent] pot 7
2013-03-13T12:13:22.513 LabV: [received] 2.300508
2013-03-13T12:13:22.514 LabV: [received] 2.112090
提前感谢任何帮助/建议。
跟进:
我后来发现,如果我使用具有相同 Java 的 10.7.5 Mac OS 机器,速度与 Windows XP 相同。我现在正在调查 10.8.3 和网络和/或安全防火墙设置的问题。再次感谢任何帮助。
【问题讨论】:
-
你可以尝试禁用nagle算法:socket.setTcpNoDelay(true)
-
感谢您的想法。但这没有帮助。
-
我怀疑它是 PrintWriter 自动刷新 导致问题,即使你
flush()强行。您可以尝试直接写入流而不是PrintWriter,例如socket.getOutputstream().write((message+"\r\n").getBytes("utf-8")); socket.getOutputstream().flush(); -
大家好。这种情况对我们来说已经很严重了,我们不知道为什么会发生这种情况。我为 LabV 端编写了一个 Java 模拟器,模拟器响应很快,所以它不是网络错误。延迟发生在 LabView 代码和读取电位器的安捷伦设备之间。我们认为问题与 Agilent 或 LabView 中的实现有关,即当我们在没有 DNS 的封闭网络中时,DNS 查找是否发生在旧代码中。我们正在考虑建立一个 DNS 以查看是否是这种情况。感谢您的帮助。
标签: java windows performance osx-lion osx-mountain-lion