1234567891011121314151617181920212223 |
- import java.util.concurrent.*;
- public class TestCallable {
- public static void main(String[] args) throws Exception {
- MyCallable callable = new MyCallable();
- FutureTask<String> future = new FutureTask<>(callable);
- Thread thread = new Thread(future);
- thread.start();
- System.out.println(future.get());
- }
- }
- class MyCallable implements Callable<String> {
- @Override
- public String call() {
- try {
- Thread.sleep(1000);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return "Finished.";
- }
- }
|