import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; // ---------------------Begin------------------------ //tips: 输出语句为:Thread.currentThread().getName()+"的call()方法在运行" //定义一个实现Callable接口的实现类,类名必须命名为MyThread_callable class MyThread_callable implements Callable{ public Object call() throws Exception { return Thread.currentThread().getName()+"的call()方法在运行"; } } // ---------------------End------------------------ public class Java { public static void main(String[] args) throws InterruptedException, ExecutionException { // 创建Callable接口的实现类对象 MyThread_callable myThread3 = new MyThread_callable(); // 使用FutureTask封装Callable接口 FutureTask<Object> ft1 = new FutureTask<>(myThread3); // 使用Thread(Runnable target ,String name)构造方法创建线程对象 Thread thread1 = new Thread(ft1, "thread1"); // 创建并启动另一个线程thread2 FutureTask<Object> ft2 = new FutureTask<>(myThread3); Thread thread2 = new Thread(ft2, "thread2"); // ---------------------Begin------------------------ // 调用线程对象的start()方法启动线程 thread1.start(); thread2.start(); // 可以通过FutureTask对象的方法管理返回 Thread.currentThread().getName() + "的call()方法在运行" = FutureTask.get(); // ---------------------End------------------------ } }
时间: 2023-08-15 22:40:26 浏览: 38
代码中的问题是,在调用 FutureTask 的 get 方法时,缺少了对应的 FutureTask 对象。正确的写法应该是:
```java
// 获取线程执行结果
Object result1 = ft1.get();
Object result2 = ft2.get();
System.out.println(result1);
System.out.println(result2);
```
此外,为了避免代码中的类型未定义,应该将 MyThread_callable 改为泛型类 MyThread_callable<T>,并在 Callable 接口中指定返回值类型。
```java
class MyThread_callable<T> implements Callable<T> {
public T call() throws Exception {
return (T) (Thread.currentThread().getName() + "的call()方法在运行");
}
}
```
相关问题
how should i finish this package step2; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; // ---------------------Begin------------------------ //tips: 输出语句为:Thread.currentThread(
).getName() + ": " + i // 实现Callable接口来创建线程 class MyCallable implements Callable<Integer> { @Override public Integer call() throws Exception { int sum = 0; for (int i = 1; i <= 100; i++) { System.out.println(Thread.currentThread().getName() + ": " + i); sum += i; } return sum; } } public class Main { public static void main(String[] args) throws InterruptedException, ExecutionException { // 创建Callable对象 MyCallable myCallable = new MyCallable(); // 将Callable对象封装成一个FutureTask对象 FutureTask<Integer> futureTask = new FutureTask<>(myCallable); // 创建线程并启动 Thread thread = new Thread(futureTask); thread.start(); // 获取线程执行结果 int result = futureTask.get(); System.out.println("线程执行结果为:" + result); } } // ----------------------End-------------------------
java FutureTask例子
可以给您提供一个简单的 Java FutureTask 的例子:
```
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class FutureTaskExample {
public static void main(String[] args) throws InterruptedException, ExecutionException {
Callable<Integer> callable = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int sum = ;
for (int i = 1; i <= 100; i++) {
sum += i;
}
return sum;
}
};
FutureTask<Integer> futureTask = new FutureTask<>(callable);
Thread thread = new Thread(futureTask);
thread.start();
int result = futureTask.get();
System.out.println("1到100的和为:" + result);
}
}
```
这个例子中,我们使用了 FutureTask 来异步执行一个 Callable 对象,然后通过 get() 方法获取 Callable 的返回值。在这个例子中,Callable 计算了 1 到 100 的和,最后返回这个和,然后我们在主线程中获取这个和并输出。