gpt4 book ai didi

java - 多线程可以运行得更快吗?

转载 作者:行者123 更新时间:2023-12-02 07:48:31 26 4
gpt4 key购买 nike

我有 2 台机器(Intel Atom(TM) CPU D525),每台都运行不同的操作系统(1 个 Windows 7 和 1 个 ubuntu 10.04)。

我希望将一堆图像从 Windows 7 计算机发送到 Ubuntu 计算机。

我现在使用多线程来做到这一点。我在下面附上了我的代码:

public class RshScp implements Runnable
{

private StreamHandlers streamHandlers = new StreamHandlers();
private String screenFileName;
private int clientIndex;
private SingletonServer ss = null;

public RshScp(String screenFileName, int clientIndex, SingletonServer ss)
{
this.screenFileName = screenFileName;
this.clientIndex = clientIndex;
this.ss = ss;
}

public void run()
{
sendFileToClient();
}

public void sendFileToClient()
{
try
{
DisplayClient dc = null;
dc = ss.getClient(clientIndex);

String execution = sshFileRSH(dc.getHostName(), dc.getUserName(), screenFileName, dc.getRemoteDirectory(), dc.getLocalDirectory());
log.write(execution);
Process p1 = Runtime.getRuntime().exec(execution);
InputStreamReader isr = new InputStreamReader(p1.getInputStream());
streamHandlers.checkStreamOutput("From RshScp", isr);
} catch(Exception e){}
}

//Function to set the RSH SCP command
private String sshFileRSH(String hostName, String userName, String localFileNames, String remoteDirName, String localJobDirectory)
{
String fileTransferCommand = "scp " + localFileNames;
//String fileTransferCommand = "rsync --partial --progress --rsh=ssh " + localFileNames[0] + " " + localFileNames[1] + " " + localFileNames[2];

String destinationCommand = userName + "@" + hostName + ":" + remoteDirName;

String executionCommand = "";
executionCommand = fileTransferCommand + " " + destinationCommand;

return executionCommand;
} // end function

}//end while class

当我尝试将文件发送到多个客户端时,我发现速度很慢。它使用 5 秒来连接并向客户端发送文件。有时仍然会丢失图像。

有谁知道到底发生了什么导致多线程变慢?有什么解决方案可以使其连接和发送速度更快吗?

最佳答案

嗯,很难准确地判断,因为您没有给我们一段有趣的代码,该代码实际上通过网络将字节发送到另一台机器。但这是我的建议:

  • 了解您的网络速度,以及将图像发送到另一台计算机并接收结果需要多长时间
  • 据此,确定在机器之间分配处理是否真的值得
  • 如果计算表明这是值得的,请不要用愚蠢的废话将数据发送到另一台计算机的方式过于复杂化。

关于java - 多线程可以运行得更快吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10527122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com