Java 如何从runnable或类似文件返回布尔值?
这是我现在的代码:Java 如何从runnable或类似文件返回布尔值?,java,return,runnable,Java,Return,Runnable,这是我现在的代码: public class discoverRunnable implements Runnable{ InetAddress address = null; boolean discovered; public discoverRunnable(InetAddress address){ this.address = address; boolean discovered = false;
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
}
}
如何返回“discovered”的值以在线程中使用它?
它应该可以在PC上工作,而不使用Android存档。您可以使用而不是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;
}
public类DiscoverRunnable实现可调用{
InetAddress=null;
布尔发现;
可公开查找(InetAddress地址){
this.address=地址;
布尔值=假;
}
@凌驾
公共布尔调用(){
//一些疯狂的东西
//may set=true
发现返回;
}
}我无法在线程完成Runnable后访问成员变量。我可以毫无问题地获得可调用的返回值future。所以,我同意,在需要从完成的runnable中获取值的情况下,始终使用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
}
}
import java.util.*;
导入java.util.concurrent.*;
班长
{
公共静态void main(字符串[]args){
ExecutorService ex=Executors.newFixedThreadPool(4);
Runnable r1=新的Runnable(){
私有布尔标志=false;
@凌驾
公开募捐{
试一试{
System.out.println(“线程:+Thread.currentThread().getName());
sleep((long)(Math.random()*1000));
flag=true;
}捕获(中断异常ie){
//无所事事
}
}
公共布尔getFlag(){
返回标志;
}
};
可调用c1=新的可调用(){
私有布尔标志=false;
@凌驾
公共布尔调用(){
试一试{
System.out.println(“线程:+Thread.currentThread().getName());
sleep((long)(Math.random()*1000));
flag=true;
}捕获(中断异常ie){
//无所事事
}
返回getFlag();
}
公共布尔getFlag(){
返回标志;
}
};
例如提交(r1);
未来f=ex.submit(c1);
例如关闭();
如果(c1!=null){
试一试{
System.out.println(“可调用的未来获取:”
+f.get();//WORKS!:显示从future返回的布尔值
System.out.println(“可调用的直接变量:”
+((可调用)c1).flag);//失败
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}
}
如果(r1!=null)System.out.println(“可运行结果:”
+((Runnable)r1).flag);//失败
}
}
在Kotlin中
fun checkWebUrl():布尔值{
val futuresult=未来任务(可调用{
webview_entry.url.startsWith(“https://developer.android.com/reference/java/util/concurrent/FutureTask")
})
返回futuresult.get()
}
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()
}