我将jar文件作为守护进程启动。它是一个简单的扫描应用程序,运行扫描文件夹的线程。我使用睡眠60000ms,所以如果我在我的Mac上运行应用程序,CPU使用率接近0%。在Ubuntu上使用jar作为守护进程使用100%cpu
如果我在我的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();
}
}
}
我开车自己的香蕉!我怎么会错过?!就像你说的那样,睡眠不在while循环的根部!谢谢! – user439781