Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. public class Transaction {
  2. private static final ConcurrentHashMap<String, Transaction> map = new ConcurrentHashMap<>();
  3. private final String id;
  4. private final Lock lock;
  5. private final Condition condition;
  6. private Orders.Order order;
  7.  
  8. private Transaction(String id) {
  9. this.id = id;
  10. this.lock = new ReentrantLock();
  11. this.condition = lock.newCondition();
  12. }
  13.  
  14. public static Transaction from(String id) {
  15. return map.computeIfAbsent(id, Transaction::new);
  16. }
  17.  
  18. public static void signalAll(String id, Orders.Order order) {
  19. Transaction transaction = map.remove(id);
  20. if (transaction != null) {
  21. transaction.lock.lock();
  22. transaction.order = order;
  23. transaction.condition.signalAll();
  24. transaction.lock.unlock();
  25. }
  26. }
  27.  
  28. public Orders.Order await() {
  29. this.lock.lock();
  30. try {
  31. this.condition.await(60, TimeUnit.SECONDS);
  32. } catch (InterruptedException e) {
  33. e.printStackTrace();
  34. }
  35. return order;
  36. }
  37.  
  38. public String getId() {
  39. return id;
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement