【发布时间】:2016-12-30 02:21:43
【问题描述】:
对不起,我的台球英语。 我有一个 javaFx 应用程序,它需要连接到许多在 android 手机中启动的 Socket 服务器(超过 40 个)。 当连接到服务器时,我创建一个线程来保持长连接,服务器每 600 毫秒将 SCREENSHOT(二进制)发送到我的应用程序。 javaFx 应用程序不能是服务器。 以下是部分代码:
while (ScreenMonitorService.isConnectionAll()){
Future<Image> f = ThreadPoolUtil.getThreadPool().submit(new Callable<Image>() {
@Override
public Image call() throws Exception {
return readImage(inputStream, outputStream);
}
});
Image fxImage = f.get();
Platform.runLater(()->{
device.getImageView().setImage(fxImage);
});
//what readImage do
private synchronized Image readImage(InputStream inputStream, OutputStream outputStream) throws IOException {
try {
Thread.sleep(700);<==== This is the now solution for high cpu performtion , but it doesn't work
} catch (InterruptedException e) {
logger.error("=====> error", e);
}
int fileLen = readInt(inputStream);
int readLength = fileLen;
int tempLength = 0;
int n;
byte[] bt = new byte[readLength];
ByteArrayOutputStream bout = new ByteArrayOutputStream();
while ((n = inputStream.read(bt,0,readLength)) > 0) {
tempLength += n;
readLength = tempLength + n > fileLen ? fileLen - tempLength : readLength;
bout.write(bt,0,n);
}
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
BufferedImage image = ImageIO.read(bin);
Image fxImage = SwingFXUtils.toFXImage(image,null);
writeInt(outputStream,1);
return fxImage;
}
我知道这是 CPU 性能不佳的原因。 我曾使用 nio|notify/wait|blockqueue 尝试解决问题,但失败了。 可能有人可以给我一些解决这个问题的建议,谢谢。
【问题讨论】: