使用简单的 Runtime.getRuntime().exec(cmd) 时代码冻结

发布于 2024-12-19 11:34:21 字数 1877 浏览 0 评论 0原文

当我运行应用程序并扫描 IP 时,它由于某种原因冻结。另外,我不确定 Yum 方法在运行命令后是否会退出,或者它是否会永远存在。我的目标是制作类似 $ ./inLinuxRunMe & 的东西,它在后台运行,当工作完成时,它会自行终止。

我不认为我的 Yum 方法正在执行此操作,因为当我开始重负载(例如播放视频等)时它会冻结。

public class MyShell
{
    public static String whatIsMyIP = null;

    public static void main(String args[])
    {
      t = new javax.swing.Timer(10000, new ActionListener() 
      {
        public void actionPerformed(ActionEvent ae) 
        {
        /* Scanner: if the ip change, show the new on too. */
        whatIsMyIP = MyShell.Yum("ifconfig eth0 | grep inet").toLowerCase());           
            /* Some logical conditions ... such as if ran then dont run, run once etc..*/ 
            bootMe();
         }
      });
      t.start();

      /* Launch: Main application server, runs forever as server */
      // MyShell.Yum("java -cp myjar.jar launch.MainApplicationAsServer");
    }

    /* Boot loader */
    public static void bootMe() throws IOException
    {
      MyShell.Yum("java -cp myjar.jar launch.MainApplicationAsServer");
    }

    /* Question: How to optimize it? So that, 
             it execute the shell command and quite*/
    public static String Yum(String cmds)
    {
        String value = "";
        try 
        {
          String cmd[] = {"/bin/sh", "-c", cmds };       
          Process p=Runtime.getRuntime().exec(cmd);       
          BufferedReader reader=new BufferedReader(
                                                                            new InputStreamReader(p.getInputStream())); 
          String line=reader.readLine(); 

          while(line!=null) 
          { 
            value += line ;
            line=reader.readLine(); 
          } 
          p.waitFor(); 

        } catch(IOException e1) {
        } catch(InterruptedException e2) { 
        }
        return value;
    }
}

When I run my application and scan for an IP it freezes for some reason. Also, I am not sure if the Yum method is exiting once it run the command, or if it lives forever. My target was to make something like $ ./inLinuxRunMe &, where it runs in the background, and when the job is done, it kills itself.

I don't think that my Yum method is doing this, because it freezes when I start heavy loads such as playing video's etc.

public class MyShell
{
    public static String whatIsMyIP = null;

    public static void main(String args[])
    {
      t = new javax.swing.Timer(10000, new ActionListener() 
      {
        public void actionPerformed(ActionEvent ae) 
        {
        /* Scanner: if the ip change, show the new on too. */
        whatIsMyIP = MyShell.Yum("ifconfig eth0 | grep inet").toLowerCase());           
            /* Some logical conditions ... such as if ran then dont run, run once etc..*/ 
            bootMe();
         }
      });
      t.start();

      /* Launch: Main application server, runs forever as server */
      // MyShell.Yum("java -cp myjar.jar launch.MainApplicationAsServer");
    }

    /* Boot loader */
    public static void bootMe() throws IOException
    {
      MyShell.Yum("java -cp myjar.jar launch.MainApplicationAsServer");
    }

    /* Question: How to optimize it? So that, 
             it execute the shell command and quite*/
    public static String Yum(String cmds)
    {
        String value = "";
        try 
        {
          String cmd[] = {"/bin/sh", "-c", cmds };       
          Process p=Runtime.getRuntime().exec(cmd);       
          BufferedReader reader=new BufferedReader(
                                                                            new InputStreamReader(p.getInputStream())); 
          String line=reader.readLine(); 

          while(line!=null) 
          { 
            value += line ;
            line=reader.readLine(); 
          } 
          p.waitFor(); 

        } catch(IOException e1) {
        } catch(InterruptedException e2) { 
        }
        return value;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽袖吟 2024-12-26 11:34:21

在单独的线程中运行 Yum() 方法。

Run you Yum() method in a separate thread.

再见回来 2024-12-26 11:34:21

来自 Java API:

Constructor Summary
Timer(int delay, ActionListener listener)
      Creates a Timer that will notify its listeners every `delay` milliseconds.

所以它每 10 秒重复一次。你需要告诉它不要重复:

public void setRepeats(boolean flag)

    If flag is false, instructs the Timer to send only one action event to its listeners.

    Parameters:
        flag - specify false to make the timer stop after sending its first action event

所以:

t = new javax.swing.Timer(10000, new ActionListener() {...});
t.setRepeats(false);
t.start();

From the Java API:

Constructor Summary
Timer(int delay, ActionListener listener)
      Creates a Timer that will notify its listeners every `delay` milliseconds.

So it repeats every 10 seconds. You need to tell it not to repeat:

public void setRepeats(boolean flag)

    If flag is false, instructs the Timer to send only one action event to its listeners.

    Parameters:
        flag - specify false to make the timer stop after sending its first action event

So:

t = new javax.swing.Timer(10000, new ActionListener() {...});
t.setRepeats(false);
t.start();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文