[英]jar as daemon on ubuntu uses 100% cpu
我将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();
}
}
}
看一下代码,花括号不太匹配,看起来您只是在文件夹中有文件时才睡觉。
尝试在该文件夹中的文件中使用守护程序代码,然后查看CPU使用率是否仍然达到峰值。
另外,如果您在代码中使用适当的缩进,它也会有所帮助。
首先,如果您发布实际上正在执行文件夹扫描的代码,则将有所帮助。 这些API在每种操作系统的VM中都有非常不同的实现,因此遇到不同的行为并不罕见。
其次,您的代码是否是连续的while循环,而没有任何线程在Java中完成睡眠? 如果是这样,那不是很好。 您应该给代码提供睡眠/屈服时间,并且应该在Java中而不是在命令行中进行,这样VM可以正确地为线程调度等操作OS调度程序。
无论如何,在OS / X上获得更好的CPU使用率的原因可能与该OS的主动抢占性有关,这不能使任何不执行任何操作的CPU变得“无用”。
---发布代码后进行编辑---
您的问题出在这行代码块中
if(filesInRoot.length>0) {
... a lot of stuff goes here ...
try
{
Thread.sleep(FileCom.PROSchedule);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}
因此,如果filesInRoot.length == 0
您不会睡觉。
您需要像这样重新排列代码
if (filesInRoot.length > 0) {
... a lot of stuff goes here ...
}
try {
Thread.sleep(FileCom.PROSchedule);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
---原始帖子如下---
也许您做出了对MacOSX有效但在Ubuntu中失败的假设。 谁知道,您可能使用了100%的CPU,因为您从未输入过sleep(...)
所在的代码块。
源代码非常适合进行有关此类主题的讨论。 没有它,一切都会变成猜谜游戏。 我们很多人都擅长猜测,但我们不喜欢这样做。 这太容易出错了,我们希望保持声誉,至少尝试提供有用的帮助。
尝试将相关代码放入一个非常小的示例程序中。 充其量,您将在完成示例之前发现并解决自己的问题。 最糟糕的是,您将有一个不同行为的有效示例,这将使其他人可以为您提供有意义的相关解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.