How to return value from thread (java)

前端 未结 5 1427
盖世英雄少女心
盖世英雄少女心 2021-01-15 11:19

I made a thread like this one bellow:

public class MyThread implements Runnable {
  private int temp;

  public MyThread(int temp){
     this.temp=temp;
  }
         


        
相关标签:
5条回答
  • 2021-01-15 11:44

    Or simply add

    ...
    a.start();
    a.join(); // Add this
    ...
    

    to wait for the thread to finish before getting the result.

    Your problem is that you're trying to get the result before it has been calculated. You should wait for the thread to finish before getting the result. This answer is perhaps not the best but is the simplest. As other people had already used the Executors class I didnt want to repeat their answers. I would, however, familiarise yourself with Thread and its methods before moving onto Executors to help you get a better understanding of threads as, from your post, it appears you may be a novice in this area.

    Thanks to l4mpi (on the meta site) for pointing out the lack of explanation.

    0 讨论(0)
  • 2021-01-15 11:46

    There are a few ways to "share" variables with Threads.

    The problem with your code is that you are passing an int which is passed by value. This means that temp and this.temp is not the same variable.

    Using a Future as other answers suggest is one way that you can share variables. Using a Future, you can ensure that the Thread has finished before actually fetching the result of that Threads execution so might be more relevant to you.

    Other ways of sharing variables among Threads that are Thread-safe, but does not guarantee that the Thread has finished execution:

    • pass an AtomicInteger and use the set method to set value.
    • use a synchronized getter method that returns the Threads value.
    0 讨论(0)
  • 2021-01-15 11:53

    You can try these code. By using Future you can hold the value return when the thread end:

    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    
    /**
     * @author mike
     * @date Sep 19, 2014
     * @description
     */
    public class Calc {
        private static class MyCallable implements Callable<Integer> {
            private int temp = 0;
    
            public MyCallable(int temp) {
                this.temp = temp;
            }
    
            @Override
            public Integer call() {
                temp += 10;
                return temp;
            }
        }
    
        public static void main(String[] args) {
            MyCallable foo = new MyCallable(10);
            try {
                Future<Integer> result = Executors.newCachedThreadPool().submit(foo);
                System.out.println(result.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-15 11:54

    In this case, you have to use Callable instead of Runnable (very similar): http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Callable.html

    public class MyThread implements Callable<Integer> { 
    
        private int temp;
    
        public MyThread(int temp){
             this.temp=temp;
        }
        @Override
        public Integer call() {
            temp+=10;
            return temp;
        }
    
    }
    
    
    public static void main(String[] args) throws InterruptedException, ExecutionException {
               ExecutorService service =  Executors.newSingleThreadExecutor();
               MyThread myTask = new MyThread(10);
               Future<Integer> future = service.submit(myTask);
               Integer result = future.get();
               System.out.println(result);
    }
    
    0 讨论(0)
  • 2021-01-15 12:03

    Use Callable instead of a Runnable, it will return a Future that you can use to retrieve the value once it is done.

    You can use a named class instead of a lambda expression if you want to.

    import java.util.concurrent.*;
    
    public class ReturnValueFromThread {
        public static void main(String[] args) throws Exception {
            ExecutorService executor = Executors.newSingleThreadExecutor();
            Future<Object> foo = executor.submit(() -> {
                return doWork();
            });
    
            System.out.println("We will reach this line before doWork is done.");
            System.out.println(foo.get()); // Will wait until the value is complete
            executor.shutdown();
        }
    
        private static double doWork() throws Exception {
            Thread.sleep(2000);
            return Math.random();
        }
    }
    
    0 讨论(0)
提交回复
热议问题