Guest User

Untitled

a guest
Apr 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. package net.abhinavsarkar.util;
  2.  
  3. public class Main {
  4.  
  5. public static void main(final String[] args) {
  6. // Use get method to get the values yielded by the generator.
  7. Fibonacci fibonacci = new Fibonacci();
  8. for (int i = 0; i < 5; i++) {
  9. System.out.println(fibonacci.get());
  10. }
  11. fibonacci.stop();
  12.  
  13. // Generator which generate values (and not only consume values)
  14. // can be iterated directly.
  15. Fibonacci fibonacciIter = new Fibonacci();
  16. int j = 0;
  17. for (Integer integer : fibonacciIter) {
  18. System.out.println(integer);
  19. if (j++ > 10) break;
  20. }
  21. fibonacciIter.stop();
  22.  
  23. // Use send method to send the value to the generator.
  24. Printer printer = new Printer();
  25. for (int i = 1; i <= 5; i++) {
  26. printer.send(i);
  27. }
  28. printer.stop();
  29.  
  30. // Use both send and get methods together. Their counterparts (yield()
  31. // and yield(Object) must be called in the same order as these are
  32. // called here. Otherwise it will result in a deadlock.
  33. Identity identity = new Identity();
  34. for (int i = 0; i < 5; i++) {
  35. identity.send(i);
  36. System.out.println(identity.get());
  37. }
  38. identity.stop();
  39.  
  40. // Non infinite generators do not need to be stopped at the end of
  41. // iteration. They stop automatically.
  42. FiveNumberGenerator fiveNumberGenerator = new FiveNumberGenerator();
  43. for (Integer integer : fiveNumberGenerator) {
  44. System.out.println(integer);
  45. }
  46. }
  47. }
Add Comment
Please, Sign In to add comment