当前位置: 代码迷 >> J2SE >> 求Callable和Runnable的示例代码,兼庆祝第一次发帖,该怎么解决
  详细解决方案

求Callable和Runnable的示例代码,兼庆祝第一次发帖,该怎么解决

热度:143   发布时间:2016-04-24 01:44:59.0
求Callable和Runnable的示例代码,兼庆祝第一次发帖
从高中注册,8年来第一次发帖!

看了java.util.concurrent包,有个问题:FutureTask(Runnable runnable, V result)怎么让返回result,求一简单示例代码。
要求:
1.使用FutureTask(Runnable runnable, V result)方法,并且返回一个非null的result。
2.一个完整的含有main(String[] args)的类。

希望能尽快结贴。48小时内如果没有满意答案也结贴。


------解决方案--------------------

------解决方案--------------------
这个FutureTask 类就是将一个Callable的转换成Runnable并执行,或者执行一个Runnable的,计算完成后返回传进的result对象.
注释掉的部分就是执行Callable的, 后面没有注释的部分是返回result的.

另外,似乎FF上不能粘源代码??
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class Task {
public static void main(String[] args){
// FutureTask<String> task = new FutureTask<String>(new Cal());
// Thread t=new Thread(task);
// t.start();
// try {
// System.out.println(task.get());
// } catch (InterruptedException e) {
// e.printStackTrace();
// } catch (ExecutionException e) {
// e.printStackTrace();
// }
FutureTask<String> task=new FutureTask<String>(new Thre(),"complete");
Thread t= new Thread(task);
t.start();
try {
System.out.println(task.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}

class Thre implements Runnable{

public void run() {
System.out.println("Runnable");
}

}
class Cal implements Callable{

public Object call() throws Exception {
System.out.println("called in Cal");
return "called";
}
}
------解决方案--------------------
package test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class TestFutureTask {
public static void main(String[] args)throws InterruptedException,ExecutionException{
final ExecutorService exec = Executors.newFixedThreadPool(5); 
Callable call = new Callable(){
public String call() throws Exception {
Thread.sleep(1000 * 5);
return "Other less important but longtime things.";
}
};
Future task = exec.submit(call);
//重要的事情
Thread.sleep(1000 * 3); 
System.out.println("Let’s do important things.");
// 其他不重要的事情
String obj = (String)task.get();
System.out.println(obj);
// 关闭线程池
exec.shutdown();
}

}
------解决方案--------------------
Thinking in java 4
上的源码
//: concurrency/CallableDemo.java
import java.util.concurrent.*;
import java.util.*;
class TaskWithResult implements Callable<String> {
private int id;
public TaskWithResult(int id) {
this.id = id;
}
public String call() {
return "result of TaskWithResult " + id;
}
}
public class CallableDemo {
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
ArrayList<Future<String>> results =
new ArrayList<Future<String>>();
for(int i = 0; i < 10; i++)
results.add(exec.submit(new TaskWithResult(i)));
for(Future<String> fs : results)
try {
// get() blocks until completion:
System.out.println(fs.get());
} catch(InterruptedException e) {
System.out.println(e);
return;
} catch(ExecutionException e) {
  相关解决方案