2011-11-16 57 views
1

我将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(); 
       } 
      } 

    } 

回答

1

看着代码,大括号不太匹配,看起来像只有在文件夹中有文件时才会睡觉。

使用文件夹中的文件尝试守护程序代码,并查看CPU使用率是否仍然出现峰值。

此外,它可以帮助您在代码中使用正确的缩进。

+0

我开车自己的香蕉!我怎么会错过?!就像你说的那样,睡眠不在while循环的根部!谢谢! – user439781

0

首先,如果您发布的是实际进行文件夹扫描的代码,这将有所帮助。这些API在每个操作系统的虚拟机中都有非常不同的实现,所以遇到不同的行为并不罕见。其次,你的代码是一个连续的while循环,没有任何线程在Java中完成睡眠?如果是这样,这不太好。您应该为您的代码提供休眠/良率,并且您应该使用Java而不是命令行来执行此操作,这样VM就会正确地为OS调度程序调度线程等。

无论如何,原因你在OS/X上获得的更好的CPU使用率可能与该操作系统的积极抢占有关,这不会让任何对CPU没有任何“有用”的东西产生作用。

0

---你发布的代码编辑后---

您的问题是在代码

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(...)所在的代码块。

源代码非常适合对此类主题进行相关讨论。没有它,一切都变成猜谜游戏。我们很多人擅长猜测,但我们不喜欢这样做。这太容易出错,我们希望至少维持声誉,试图提供有用的帮助。

尝试将相关代码放在一个非常小的示例程序中。充其量,在完成示例之前,您会发现并解决您自己的问题。在最坏的情况下,你会有一个不同行为的实例,这将允许其他人为你提供有意义的相关解决方案。

相关问题