TestCallable.java 598 B

1234567891011121314151617181920212223
  1. import java.util.concurrent.*;
  2. public class TestCallable {
  3. public static void main(String[] args) throws Exception {
  4. MyCallable callable = new MyCallable();
  5. FutureTask<String> future = new FutureTask<>(callable);
  6. Thread thread = new Thread(future);
  7. thread.start();
  8. System.out.println(future.get());
  9. }
  10. }
  11. class MyCallable implements Callable<String> {
  12. @Override
  13. public String call() {
  14. try {
  15. Thread.sleep(1000);
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. return "Finished.";
  20. }
  21. }