Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. package com.techpredators.threads;
  2.  
  3. import java.util.concurrent.Callable;
  4. import java.util.concurrent.ExecutionException;
  5. import java.util.concurrent.ExecutorService;
  6. import java.util.concurrent.Executors;
  7. import java.util.concurrent.Future;
  8. import java.util.stream.Stream;
  9.  
  10. public class CallableExample {
  11. public static void main(String[] args) throws Exception {
  12.  
  13. try {
  14. ExecutorService service = Executors.newSingleThreadExecutor();
  15. Stream<String> stream = Stream.of("abc", "def", "ghi", "jkl");
  16. SampleTask sumTask = new SampleTask(stream);
  17. Future<Long> future = service.submit(sumTask);
  18. Long result = future.get();
  19. System.out.println(result);
  20. } catch (InterruptedException interruptedException) {
  21. System.out.println("InterruptedException occured "
  22. + interruptedException.getMessage());
  23. } catch (ExecutionException executionException) {
  24. System.out.println("ExecutionException occured "
  25. + executionException.getMessage());
  26. }
  27. }
  28. }
  29.  
  30. class SampleTask implements Callable<Long> {
  31. private Stream<String> stream = null;
  32.  
  33. public SampleTask(Stream<String> stream) {
  34. this.stream = stream;
  35. }
  36.  
  37. @Override
  38. public Long call() throws Exception {
  39. return this.stream.count();
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement