Java Multithreading returns function results

Source: Internet
Author: User

Two ways: an Inheritance thread class implementation, one by implementing the callable interface.

The first method:

Because the Run method that implements the thread class itself does not have a return value, it is not possible to get the execution result of the thread directly, but you can get the value of the instance variable by passing the final result to the instance variable in the Run method, and then using the Getxx method. Code that inherits the implementation:

[Java]View Plaincopy
  1. Class Runthread extends thread{
  2. private String Runlog;
  3. private BufferedReader BR;
  4. {
  5. Runlog = "";
  6. }
  7. Public Runthread (BufferedReader br) {
  8. this.br = BR;
  9. }
  10. public Void Run () {
  11. try {
  12. String output = "";
  13. While (output = Br.readline ()) = null) {
  14. This.runlog + = output + "\ n";
  15. }
  16. } catch (IOException e) {
  17. E.printstacktrace ();
  18. }
  19. }
  20. Public String Getrunlog () {
  21. return This.runlog;
  22. }
  23. }


The second method:

The call method needs to be implemented after inheriting the callable interface, which can have a return value by default, so you can return directly to what you want to return. Implementation code for the interface:

[Java]View Plaincopy
  1. Class Callthread implements callable<string>{
  2. private String Runlog;
  3. private BufferedReader BR;
  4. {
  5. Runlog = "";
  6. }
  7. Public Callthread (BufferedReader br) {
  8. this.br = BR;
  9. }
  10. @Override
  11. Public String Call () throws Exception {
  12. try {
  13. String output = "";
  14. While (output = Br.readline ()) = null) {
  15. Runlog + = output + "\ n";
  16. }
  17. } catch (IOException e) {
  18. return runlog;
  19. }
  20. return runlog;
  21. }
  22. }


The following is called.

The first way of calling code:

[Java]View Plaincopy
  1. Runthread Th1 = new Runthread (standout);
  2. Th1.start ();
  3. Runthread Th2 = new Runthread (STANDERR);
  4. Th2.start ();
  5. Th2.join (); //Ensure that the previous 2 threads end before the main process, otherwise the obtained result may be empty or incomplete
  6. Runlog + = Th1.getrunlog () + "\ n";
  7. Runlog + = Th2.getrunlog () + "\ n";


The second way of calling code:

[Java]View Plaincopy
    1. Executorservice Exs = Executors.newcachedthreadpool ();
    2. Arraylist<future<string>> al = new arraylist<future<string>> ();
    3. Al.add (Exs.submit (new Callthread (standout)));
    4. Al.add (Exs.submit (new Callthread (Standerr)));
    5. For (future<string> fs:al) {
    6. try {
    7. Runlog + = Fs.get () + "\ n";
    8. } catch (Interruptedexception e) {
    9. E.printstacktrace ();
    10. } catch (Executionexception e) {
    11. E.printstacktrace ();
    12. }
    13. }

Java Multithreading returns function results

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.