Guest User

Untitled

a guest
Jul 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. public interface GeolocationListener {
  2. public void onResult(Address[] addresses);
  3. public void onError(Exception e);
  4. }
  5.  
  6. public class SignGuestbookServlet extends HttpServlet {
  7.  
  8. public void doPost(HttpServletRequest req, HttpServletResponse resp)
  9. throws IOException {
  10. // ...
  11. resp.sendRedirect("/guestbook.jsp");
  12. }
  13. }
  14.  
  15. Response a = getResponse();
  16. // wait until the response is received, do not go further
  17. // process
  18. Response b = getResponse();
  19. // wait until the response is received, do not go further
  20. process(a,b);
  21.  
  22. private Address getFullAddress (String text, AddressListener listener, ... ){
  23.  
  24. // new Geolocation(text, listener, options).start()
  25. // implements Geolocation.GeolocationListener
  26. // how to return the Address from the onResult ?
  27. }
  28.  
  29. import java.util.concurrent.*;
  30.  
  31. public class MakeAsynchronousCodeSynchronous {
  32. public static void main(String[] args) throws Exception {
  33. final Listener listener = new Listener();
  34. Runnable delayedTask = new Runnable() {
  35. @Override
  36. public void run() {
  37. try {
  38. Thread.sleep(2000);
  39. } catch (InterruptedException e) {
  40. throw new IllegalStateException("Shouldn't be interrupted", e);
  41. }
  42. listener.onResult(123);
  43.  
  44. }
  45. };
  46. System.out.println(System.currentTimeMillis() + ": Starting task");
  47. Executors.newSingleThreadExecutor().submit(delayedTask);
  48. System.out.println(System.currentTimeMillis() + ": Waiting for task to finish");
  49. while (!listener.isDone()) {
  50. Thread.sleep(100);
  51. }
  52. System.out.println(System.currentTimeMillis() + ": Task finished; result=" + listener.getResult());
  53. }
  54.  
  55. private static class Listener {
  56. private Integer result;
  57. private boolean done;
  58.  
  59. public void onResult(Integer result) {
  60. this.result = result;
  61. this.done = true;
  62. }
  63.  
  64. public boolean isDone() {
  65. return done;
  66. }
  67.  
  68. public Integer getResult() {
  69. return result;
  70. }
  71. }
  72. }
  73.  
  74. ExecutorService es = Executors.newFixedThreadPool(2);
  75. ...
  76. Future<Response> responseA = es.submit(responseGetter);
  77. Future<Response> responseB = es.submit(responseGetter);
  78.  
  79. process(responseA.get(), responseB.get());
  80.  
  81. CountDownLatch latch = new CountDownLatch(1);
  82. Response a = getResponse(latch);
  83. latch.await();
  84.  
  85. latch = new CountDownLatch(1);
  86. Response b = getResponse(latch);
  87. latch.await();
  88.  
  89. process(a, b);
  90.  
  91. public Response getResponse(CountDownLatch latch) {
  92. someAsychBloc(final CountDownLatch latch) {
  93. do work
  94. latch.countDown();
  95. }
  96. }
Add Comment
Please, Sign In to add comment