public class CallThread implements Callable {
private String name;
private int age;
private double money;
public CallThread(String name, Integer age, double money) {
this.name = name;
this.age = age;
this.money = money;
}
@Override
public Double call() throws Exception {
// 花费100元
money -= 100;
// 返回余额
return money;
}
}
public class ThreadTest {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask futureTask = new FutureTask<>(new CallThread("小明", 22, 5000));
futureTask.run();
System.out.println("小明还剩余额:" + futureTask.get());
}
}
运行如下:
E:\software\jdk\jdk17\bin\java.exe "-javaagent:E:\software\IntelliJ IDEA 2022.2\lib\idea_rt.jar=52880:E:\software\IntelliJ IDEA 2022.2\bin" -Dfile.encoding=UTF-8 -classpath E:\workSpace\aws-doc-sdk-examples\java\example_code\JavaTest\target\classes com.rojer.test.threadtest.ThreadTest
小明还剩余额:4900.0
Process finished with exit code 0