|
| 1 | +package com.learnjava.thread.reentranlock; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | +import java.util.Objects; |
| 5 | +import java.util.concurrent.ConcurrentHashMap; |
| 6 | +import java.util.concurrent.TimeUnit; |
| 7 | +import java.util.concurrent.locks.Condition; |
| 8 | +import java.util.concurrent.locks.Lock; |
| 9 | +import java.util.concurrent.locks.ReentrantLock; |
| 10 | + |
| 11 | +/** |
| 12 | + * 模拟客户端请求类,用于构建请求对象 |
| 13 | + * |
| 14 | + * @author lhy |
| 15 | + * @date 2022/2/10 |
| 16 | + */ |
| 17 | +public class RequestFuture { |
| 18 | + public static Map<Long, RequestFuture> futures = new ConcurrentHashMap<>(); |
| 19 | + private final Lock lock = new ReentrantLock(); |
| 20 | + private final Condition condition = lock.newCondition(); |
| 21 | + private long id; |
| 22 | + /** |
| 23 | + * 请求参数 |
| 24 | + */ |
| 25 | + private Object request; |
| 26 | + /** |
| 27 | + * 响应结果 |
| 28 | + */ |
| 29 | + private Object result; |
| 30 | + /** |
| 31 | + * 超时时间 |
| 32 | + */ |
| 33 | + private long timeout = 5000; |
| 34 | + |
| 35 | + /** |
| 36 | + * 把请求放入本地缓存中 |
| 37 | + * @param future |
| 38 | + */ |
| 39 | + public static void addFuture(RequestFuture future) { |
| 40 | + futures.put(future.getId(), future); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * 同步获取响应结果 |
| 45 | + * @return |
| 46 | + */ |
| 47 | + public Object get() { |
| 48 | + lock.lock(); |
| 49 | + try { |
| 50 | + while (this.result == null) { |
| 51 | + try { |
| 52 | + // 主线程默认等待5s,然后查看下结果 |
| 53 | + condition.await(timeout, TimeUnit.MILLISECONDS); |
| 54 | + } catch (InterruptedException e) { |
| 55 | + e.printStackTrace(); |
| 56 | + } |
| 57 | + } |
| 58 | + } finally { |
| 59 | + lock.unlock(); |
| 60 | + } |
| 61 | + return this.result; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * 异步线程将结果返回主线程 |
| 66 | + * @param result |
| 67 | + */ |
| 68 | + public static void received(Response result) { |
| 69 | + RequestFuture future = futures.remove(result.getId()); |
| 70 | + if (null != future) { |
| 71 | + future.setResult(result.getResult()); |
| 72 | + } |
| 73 | + /** |
| 74 | + * 通知主线程 |
| 75 | + */ |
| 76 | + Objects.requireNonNull(future, "RequestFuture").getLock().lock(); |
| 77 | + try { |
| 78 | + future.getCondition().signalAll(); |
| 79 | + } finally { |
| 80 | + Objects.requireNonNull(future, "RequestFuture").getLock().unlock(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public long getId() { |
| 85 | + return id; |
| 86 | + } |
| 87 | + |
| 88 | + public void setId(long id) { |
| 89 | + this.id = id; |
| 90 | + } |
| 91 | + |
| 92 | + public Object getRequest() { |
| 93 | + return request; |
| 94 | + } |
| 95 | + |
| 96 | + public void setRequest(Object request) { |
| 97 | + this.request = request; |
| 98 | + } |
| 99 | + |
| 100 | + public Object getResult() { |
| 101 | + return result; |
| 102 | + } |
| 103 | + |
| 104 | + public void setResult(Object result) { |
| 105 | + this.result = result; |
| 106 | + } |
| 107 | + |
| 108 | + public long getTimeout() { |
| 109 | + return timeout; |
| 110 | + } |
| 111 | + |
| 112 | + public void setTimeout(long timeout) { |
| 113 | + this.timeout = timeout; |
| 114 | + } |
| 115 | + |
| 116 | + public Lock getLock() { |
| 117 | + return lock; |
| 118 | + } |
| 119 | + |
| 120 | + public Condition getCondition() { |
| 121 | + return condition; |
| 122 | + } |
| 123 | +} |
0 commit comments