package step2; 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<T> implements Callable<T> { public T call() throws Exception { return (T) (Thread.currentThread().getName() + "的call()方法在运行"); }} // ---------------------End------------------------ public class CallableExample { 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()方法启动线程 System.out.println(thread1.start()) ; System.out.println(thread2.start()) ; // 可以通过FutureTask对象的方法管理返回值 Object result1 = ft1.get(); Object result2 = ft2.get(); System.out.println(result1); System.out.println(result2); // ---------------------End------------------------ } }
时间: 2023-08-15 10:48:04 浏览: 67
这段代码是一个Java程序,主要演示了如何使用Callable和FutureTask接口创建多线程,并且在主线程中获取子线程的返回值。程序中定义了一个实现了Callable接口的类MyThread_callable,同时使用FutureTask封装了该类,然后创建了两个线程对象并启动它们,最后通过FutureTask对象的get()方法获取线程的返回值并输出。需要注意的是,Callable接口的call()方法可以具有返回值,而Runnable接口的run()方法则没有返回值。
相关问题
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-------------------------
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------------------------ } }
代码中的问题是,在调用 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()方法在运行");
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)