开发者

Load function in a thread

开发者 https://www.devze.com 2023-02-25 09:35 出处:网络
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 */

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... start开发者_JS百科s, 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();


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


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) {}
}


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();


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

0

精彩评论

暂无评论...
验证码 换一张
取 消