Use of return values in Java threads

Source: Internet
Author: User

http://icgemu.iteye.com/blog/467848

Use of return values in Java threads Blog Category:
    • Java
Javathread sometimes requires a value in the thread to be returned in the threads, and in general we will use the Runnable interface and the thread class to set a variable, change the value of the variable in run (), and use a Get method to get the value, but when the run finishes is unknown ; We need some mechanism to ensure that.

In Java SE5 There is a callable interface, we can use this interface to complete the function;

Code such as:
Java code
  1. Package com.threads.test;
  2. Import java.util.concurrent.Callable;
  3. Public class Callablethread implements callable<string> {
  4. private String str;
  5. private int count=10;
  6. Public callablethread (String str) {
  7. This.str=str;
  8. }
  9. //need to implement the call method for callable
  10. Public String Call () throws Exception {
  11. For (int i=0;i<this.count;i++) {
  12. System.out.println (this.str+"" +i);
  13. }
  14. return this.str;
  15. }
  16. }


Executing the same task in the call method as in the run () method, unlike call (), which has a return value.
The return type of call should be the same as the generic type of callable<t>.
The test code is as follows:
Java code
  1. Package com.threads.test;
  2. Import java.util.ArrayList;
  3. Import java.util.concurrent.ExecutionException;
  4. Import Java.util.concurrent.ExecutorService;
  5. Import java.util.concurrent.Executors;
  6. Import Java.util.concurrent.Future;
  7. Public class Callabletest {
  8. public static void Main (string[] args) {
  9. Executorservice Exs=executors.newcachedthreadpool ();
  10. arraylist<future<string>> al=New arraylist<future<string>> ();
  11. For (int i=0;i<10;i++) {
  12. Al.add (Exs.submit (new Callablethread ("String" +i));
  13. }
  14. For (future<string> fs:al) {
  15. try {
  16. System.out.println (Fs.get ());
  17. } catch (Interruptedexception e) {
  18. E.printstacktrace ();
  19. } catch (Executionexception e) {
  20. E.printstacktrace ();
  21. }
  22. }
  23. }
  24. }

The Submit () method returns a Futrue object that can be returned by calling the Get method of the object.
This method can be used to handle the problem of the return value in the thread very well.
    • Callable.jar (3.9 KB)
    • Download number of times: 91

Use of return values in Java threads

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.