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

标签 java ubuntu jar cpu daemon

我将一个 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/

相关文章:

c++ - 在 linux ubuntu 服务器 11.10 (vps) 上使用 asio 使用 C++ 进行编译

java - Inno 安装程序 : Removing files installed by previous version

java - 运行 .jar 时的 getResourceAsStream 文件路径

java - NetBeans 中与 Maven 的跨项目依赖关系

java - AWS SQS : java. lang.NoClassDefFoundError : com/fasterxml/jackson/annotation/JsonMerge?

java - 没有创建 liquibase.change.core.RawSQLChange 的逆

ubuntu - 解决xinetd "Transport endpoint is not connected"

django - 访问本地网络中 ubuntu 服务器上托管的网页

java - 合并两个流

java - 将 JAR 中的文件包含在依赖项目中