import java.util.concurrent.Callable;
public class CallableDemo implements Callable<String> {
@Override
public String call() throws Exception {
String name = "meizi";
return name;
}
}
//测试类
public class TestCallable {
/**
* @param args
*/
public static void main(String[] args) {
Callable<String> oneCallable = new CallableDemo();
FutureTask<String> future = new FutureTask<String>(oneCallable);
new Thread(future).start();
try {
System.out.println(future.get());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
还有一种执行线程的方法,不知道怎么弄,亲们给我看看,贴下代码 ? |