How to return a boolean from a runnable or similar?
Asked Answered
E

3

6

This is my code by now:

public class discoverRunnable implements Runnable{
      InetAddress address = null;
      boolean discovered;

      public discoverRunnable(InetAddress address){
            this.address = address;
            boolean discovered = false;
      }

      @Override
      public void run(){
            //some crazy stuff
            //may set discovered = true
      }
}

How can I return the Value of "discovered" to use it within a Thread now? It should work on a PC without using Android archives.

Erupt answered 12/11, 2015 at 15:12 Comment(0)
A
9

You can use Callable instead of Runnable

public class DiscoverRunnable implements Callable<Boolean> {
  InetAddress address = null;
  boolean discovered;

  public DiscoverRunnable(InetAddress address){
        this.address = address;
        boolean discovered = false;
  }

  @Override
  public Boolean call(){
        //some crazy stuff
        //may set discovered = true
     return discovered;
  }

}

Antiquity answered 12/11, 2015 at 15:13 Comment(0)
H
1

I couldn't get a member variable to be accessible after a thread finishes a Runnable. I can get the Callable to return future with value, without a problem. So, I would agree, always use a Callable in these cases where you need a value from a finished runnable.

import java.util.*;
import java.util.concurrent.*;

class Main
{
    public static void main(String[] args) {
        ExecutorService ex = Executors.newFixedThreadPool(4);

        Runnable r1 = new Runnable() {
            private boolean flag = false;
            @Override
            public void run() {
                try {
                    System.out.println("Thread: " + Thread.currentThread().getName());
                    Thread.sleep((long)(Math.random() * 1000));
                    flag = true;
                } catch (InterruptedException ie) {
                    // do nothing
                }
            }
            public boolean getFlag() {
                return flag;
            }
        };

        Callable<Boolean> c1 = new Callable<Boolean>() {
            private boolean flag = false;
            @Override
            public Boolean call() {
                try {
                    System.out.println("Thread: " + Thread.currentThread().getName());
                    Thread.sleep((long)(Math.random() * 1000));
                    flag = true;
                } catch (InterruptedException ie) {
                    // do nothing
                }
                return getFlag();
            }
            public boolean getFlag() {
                return flag;
            }
        };

        ex.submit(r1);
        Future<Boolean> f = ex.submit(c1);

        ex.shutdown();

        if (c1 != null) {
            try {
                System.out.println("Callable future-get: " 
  + f.get()); //WORKS!: shows boolean value returned from future
                System.out.println("Callable direct-var: " 
  + ((Callable<Boolean>) c1).flag); //FAIL
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        if (r1 != null) System.out.println("Runnable result: " 
  + ((Runnable) r1).flag); //FAIL
    }
}
Hydrogenous answered 11/4, 2018 at 21:25 Comment(0)
A
0
In Kotlin
 
fun checkWebUrl(): Boolean {

        val futureResult = FutureTask<Boolean>(Callable<Boolean> {
            webview_entry.url.startsWith("https://developer.android.com/reference/java/util/concurrent/FutureTask")
        })
        
        return futureResult.get()
    }
Anglaangle answered 13/7, 2020 at 2:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.