在线程中加载函数

发布于 2024-11-01 04:57:57 字数 1616 浏览 0 评论 0原文

我如何使用 callme(input);启动一个新线程?

  /* We send username and password for register and load a heavy load */
  public class button3 implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {      
      String input = output.getText();
      if (input.length()<=0)
      {
        JOptionPane.showMessageDialog(null, "Empty....");
      } else {
        callme(input);
      }
    }
  }

  public static String callme() { //heavy loads... starts, which freezed the button 
     return "Did you called me?";
  }

尝试1:但是失败(output1没有得到返回的文本结果):

  /* We send username and password for register and nat mapping */
  public class button3 implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {      
      String input = output.getText();
      if (input.length()<=0)
      {
        JOptionPane.showMessageDialog(null, "Empty....");
      } else {

        new Thread(new Runnable() {
          public void run() {
            try {
              output1.setText( callme(output.getText()) );
            } catch(Exception t) {
            }
          }
        }).start();


      }
    }
  }

尝试2:也尝试了这个,没有返回output1 = callme();

new Thread(new Runnable() {
  public void run() {
    final String result = callme(output.getText());
    SwingUtilities.invokeLater(new Runnable() {      
      public void run() {
        try {
            output1.setText( result );
        } catch(Exception t) {
        }
      }
    });
  }
}).start();

How can i use the callme(input); to get launched with a new thread?

  /* We send username and password for register and load a heavy load */
  public class button3 implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {      
      String input = output.getText();
      if (input.length()<=0)
      {
        JOptionPane.showMessageDialog(null, "Empty....");
      } else {
        callme(input);
      }
    }
  }

  public static String callme() { //heavy loads... starts, which freezed the button 
     return "Did you called me?";
  }

Try 1: but getting failed (output1 does not get the returned text results):

  /* We send username and password for register and nat mapping */
  public class button3 implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {      
      String input = output.getText();
      if (input.length()<=0)
      {
        JOptionPane.showMessageDialog(null, "Empty....");
      } else {

        new Thread(new Runnable() {
          public void run() {
            try {
              output1.setText( callme(output.getText()) );
            } catch(Exception t) {
            }
          }
        }).start();


      }
    }
  }

Try 2: Also tried this, did not returns output1 = callme();

new Thread(new Runnable() {
  public void run() {
    final String result = callme(output.getText());
    SwingUtilities.invokeLater(new Runnable() {      
      public void run() {
        try {
            output1.setText( result );
        } catch(Exception t) {
        }
      }
    });
  }
}).start();

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

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

发布评论

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

评论(4

找个人就嫁了吧 2024-11-08 04:57:57
new Thread(new Runnable() {
  public void run() {
    try {
      callme(input);
    } catch(Exception t) {
       // appropriate error reporting here
    }
  }
}).start();

请注意,input 必须声明为 final

另外,请考虑使用 Swing 实用程序

new Thread(new Runnable() {
  public void run() {
    try {
      callme(input);
    } catch(Exception t) {
       // appropriate error reporting here
    }
  }
}).start();

Note that input must be declared as final.

Also, consider using invokeLater(Runnable) from Swing Utilities

纵性 2024-11-08 04:57:57

试试这个:

public class button3 implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        final String input = output.getText();
        if ( input.length() <= 0 ) {
            JOptionPane.showMessageDialog(null, "Empty....");
        }
        else {
            Thread t = new Thread(new Runnable() {
                public void run() {
                    callme(input);
                }
            });
            t.start();
        }
    }

    public static String callme(String input) {}
}

Try this:

public class button3 implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        final String input = output.getText();
        if ( input.length() <= 0 ) {
            JOptionPane.showMessageDialog(null, "Empty....");
        }
        else {
            Thread t = new Thread(new Runnable() {
                public void run() {
                    callme(input);
                }
            });
            t.start();
        }
    }

    public static String callme(String input) {}
}
小帐篷 2024-11-08 04:57:57

如果您修改方法内的摆动控件,您应该使用

new Thread(new Runnable() {

    @Override
    public void run() {

        final String result = callme(input);

        SwingUtilities.invokeLater(new Runnable() {      

            @Override
            public void run() {
                try {
                    output1.setText( result );
                } catch(Exception t) {
                }
            }
        });
    }
}).start();

If you modify swing controls inside your method you should use

new Thread(new Runnable() {

    @Override
    public void run() {

        final String result = callme(input);

        SwingUtilities.invokeLater(new Runnable() {      

            @Override
            public void run() {
                try {
                    output1.setText( result );
                } catch(Exception t) {
                }
            }
        });
    }
}).start();
对不⑦ 2024-11-08 04:57:57

如果您需要返回值,您实际上应该使用带有 ExecutorService 的 Callable ,它将返回一个 Future,您可以稍后使用它来检索值。

请参阅:

http://download.oracle.com /javase/6/docs/api/java/util/concurrent/Executors.html

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html

If you need the return value, you should really be using a Callable with an ExecutorService which will give you back a Future that you can use the retrieve the value later on.

See:

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Executors.html

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文