gpt4 book ai didi

java - jar 作为 ubuntu 上的守护进程使用 100% cpu

转载 作者:太空宇宙 更新时间:2023-11-03 17:01:52 24 4
gpt4 key购买 nike

我将一个 jar 文件作为守护进程启动。它是一个简单的扫描应用程序,运行一个扫描文件夹的线程。我使用 sleep 60000ms,所以如果我在我的 mac 上运行应用程序,cpu 使用率接近 0%。

如果我在我的 32b Ubuntu 服务器上将 jar 作为守护进程运行,它会在空闲时消耗 100% 的 cpu(例如,它扫描的文件夹中没有文件)。

sudo start-stop-daemon --start --quiet -b -m --pidfile /var/run/filecom.pid --exec /usr/bin/java -- -Xms128m -Xmx128m -jar /apps/FileCom/filecom.jar

我做错了什么?

谢谢

编辑

我做了一个 Thread.sleep(60000)。当我不将它作为守护进程运行时,它不会消耗那么多 cpu。我的猜测是它与我的守护进程有关。

public void run() 
{
//Create our root folder (folder to scan)
File root = new File(rootFolder);

//Start the loop
while(true)
{
//List all files in the root folder
File[] filesInRoot = root.listFiles();

Arrays.sort( filesInRoot, new Comparator<Object>()
{
public int compare(Object o1, Object o2)
{
if (((File)o1).lastModified() < ((File)o2).lastModified())
{
return -1;
}
else if (((File)o1).lastModified() > ((File)o2).lastModified())
{
return +1;
}
else
{
return 0;
}
}

});

//If there are no files in the target folder then move the first file in the array
if(filesInRoot.length>0)
{

LogUtil.write(">> Finds file in in queue: " + filesInRoot[0].getName());

//Check that the file has been written, EOF
if(checkEOF(filesInRoot[0]))
{
LogUtil.write(">> File is complete, preparing to move");
//Rename the file using time and date - to make it unique
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Long time = cal.getTimeInMillis();
String fileprefix = sdf.format(cal.getTime())+time.toString();
String processFileName = fileprefix+"_"+filesInRoot[0].getName();
//Move the file and rename it
File processFile = new File(processFolder, processFileName);

boolean success = filesInRoot[0].renameTo(processFile);

if (success)
{
LogUtil.write(">> Success: File was moved to "+processFolder);
LogUtil.write(">> Processing....");


try
{
//Do stuff

}
catch (Exception e) //Handles all errors
{

LogUtil.write(e);
}
finally
{
//Create backup of the infile (outfile is bupped in writeResponseObject)
File bupInFile = new File(bupFolder+"/in", processFileName);
processFile.renameTo(bupInFile);
LogUtil.write(">> inFile backed up: "+bupInFile.getAbsolutePath());
}
}
else
{
LogUtil.write(">> Failure: File could not be moved ");
}
}
else
{
LogUtil.write(">> Failure: file is still beeing written...");
}
try
{
Thread.sleep(FileCom.PROSchedule);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}

}

最佳答案

查看代码,大括号不太匹配,看起来你只是在文件夹中有文件时才在 sleep 。

使用文件夹中的文件尝试您的守护程序代码,看看 CPU 使用率是否仍然达到峰值。

此外,如果您在代码中使用适当的缩进,也会有所帮助。

关于java - jar 作为 ubuntu 上的守护进程使用 100% cpu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8157805/

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