如何 return 来自可运行对象或类似对象的布尔值?
How to return a boolean from a runnable or similar?
这是我现在的代码:
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
}
}
现在如何 return "discovered" 的值在线程中使用它?
它应该可以在不使用 Android 存档的 PC 上运行。
您可以使用 Callable 而不是 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;
}
}
我无法在线程完成 Runnable 后访问成员变量。我可以毫无问题地获得 Callable to return future 的价值。所以,我同意,在这些需要已完成的可运行对象的值的情况下,始终使用 Callable。
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
}
}
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()
}
这是我现在的代码:
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
}
}
现在如何 return "discovered" 的值在线程中使用它? 它应该可以在不使用 Android 存档的 PC 上运行。
您可以使用 Callable 而不是 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;
}
}
我无法在线程完成 Runnable 后访问成员变量。我可以毫无问题地获得 Callable to return future 的价值。所以,我同意,在这些需要已完成的可运行对象的值的情况下,始终使用 Callable。
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
}
}
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()
}