下面列出了java.util.concurrent.locks.ReentrantLock#newCondition() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Message call() throws Exception {
try {
CReqCallBack reqCallBack = (CReqCallBack) request;
int index = CallBack.nextMsgId();
reqCallBack.setIndex(index);
reqCallBack.serialize();
session.sendMessage(reqCallBack);
ReentrantLock lock = new ReentrantLock();
Condition condition = lock.newCondition();
long maxTimeOut = 5 * TimeUtil.ONE_SECOND;
long startTime = System.currentTimeMillis();
CallBackService callBackService = CallBackService.getInstance();
while (System.currentTimeMillis() - startTime <= maxTimeOut) {
CallBack c = callBackService.removeCallBack(index);
if (c != null) {
return c.getData();
}
try {
lock.lockInterruptibly();
condition.await(10, TimeUnit.MILLISECONDS);
} catch (Exception e) {
} finally {
lock.unlock();
}
}
} finally {
C2SSessionPoolFactory.getInstance().returnSession(session);
}
// 超时返回
return null;
}
public FirmataAdapter(FirmataEventListener eventListener) {
this.eventListener = eventListener;
running = new AtomicBoolean(false);
responseQueue = new ConcurrentLinkedQueue<>();
lock = new ReentrantLock();
condition = lock.newCondition();
pins = new ConcurrentHashMap<>();
adcToGpioMapping = new ConcurrentHashMap<>();
executor = Executors.newSingleThreadExecutor();
}
@Test
void testInterruptDuringRetrySleep() throws InterruptedException {
ReentrantLock lock = new ReentrantLock();
Condition noConnectionCondition = lock.newCondition();
when(consumerContainerMock.isConnectionAvailable()).thenReturn(true);
when(consumerContainerMock.ensureConsumersAreActive()).thenReturn(false);
ConsumerContainerWatcherThread consumerContainerWatcherThread =
new ConsumerContainerWatcherThread(consumerContainerMock, 1000000, lock,
noConnectionCondition);
consumerContainerWatcherThread.start();
Thread.sleep(350);
assertTrue(consumerContainerWatcherThread.isAlive());
killThreadAndCheckState(consumerContainerWatcherThread);
}
public SingletonValue(SingletonBuilder<T> builder) {
this.builder = builder;
state = ValueState.NOT_SET;
sync = new ReentrantLock();
change = sync.newCondition();
}
public void testHasWaitersIMSE(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
final Condition c = lock.newCondition();
try {
lock.hasWaiters(c);
shouldThrow();
} catch (IllegalMonitorStateException success) {}
}
public ConnectionPoolManager(int poolSize) {
this.maxPoolSize = poolSize;
availableClients = new LinkedList<ClientWrapper>();
checkedOutClients = new HashSet<ClientWrapper>();
poolLock = new ReentrantLock();
availableClientsCondition = poolLock.newCondition();
currentPoolSize = 0;
}
/**
* Creates a new instance of AbstractManualActionContext with the supplied engine, action and
* source bindings.
*
* @param engine
* Parent engine.
* @param action
* The action this context is for.
* @param sourceBindings
* Bindings to shallow copy.
*/
protected AbstractManualActionContext(final ActionEngine engine, final Action<T> action,
final ActionBindings sourceBindings) {
super(engine, action, sourceBindings);
lock = new ReentrantLock();
ran = lock.newCondition();
done = new AtomicBoolean();
performing = new AtomicBoolean();
cancelled = new AtomicBoolean();
estimated = new AtomicLong();
unschedulable = unschedulableActionContext(this);
}
@Test
public void testCondition() throws InterruptedException {
ReentrantLock lock = new ReentrantLock();
Condition condition = lock.newCondition();
Thread thread = new Thread(new ConditionTask(lock, condition));
thread.start();
Thread.sleep(2000);
lock.lock();
condition.signal();
lock.unlock();
}
public GifDecoder(GifAction gifaction)
{
isDestroy = false;
f = 1;
A = new byte[256];
B = 0;
C = 0;
D = 0;
E = false;
F = 0;
O = new ArrayBlockingQueue(15);
P = new ReentrantLock();
Q = P.newCondition();
R = P.newCondition();
S = 0;
T = false;
U = new ArrayList(M);
V = 0;
W = false;
X = null;
Y = null;
Z = false;
aa = 0;
ab = null;
ac = 0;
ad = null;
ae = null;
af = new int[256];
X = gifaction;
}
public LcdGame(LcdConnection lcdConnection, int leftGpio, int rightGpio, int okGpio) {
random = new Random();
lcd = new HD44780Lcd(lcdConnection, 20, 4);
leftButton = new Button(leftGpio);
rightButton = new Button(rightGpio);
okButton = new Button(okGpio);
lock = new ReentrantLock();
cond = lock.newCondition();
leftButton.whenReleased(this::leftReleased);
rightButton.whenReleased(this::rightReleased);
okButton.whenReleased(this::okReleased);
}
public void testGetWaitQueueLengthIAE(boolean fair) {
final ReentrantLock lock = new ReentrantLock(fair);
final Condition c = lock.newCondition();
final ReentrantLock lock2 = new ReentrantLock(fair);
try {
lock2.getWaitQueueLength(c);
shouldThrow();
} catch (IllegalArgumentException success) {}
}
protected Task() {
lock = new ReentrantLock();
condition = lock.newCondition();
}
public BlockTask() {
lock = new ReentrantLock();
condition = lock.newCondition();
notify = false;
remove = false;
}
PausableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
threadLock = new ReentrantLock();
condition = threadLock.newCondition();
}
public BlockingSet() {
set = new HashSet<T>();
lock = new ReentrantLock();
unlock = lock.newCondition();
}
public StateMachineImpl()
{
mStateLock = new ReentrantLock();
mStateChangedCondition = mStateLock.newCondition();
mState = new StateUninitialized(this);
}
/**
* Creates an {@code ArrayBlockingQueue} with the given (fixed)
* capacity and the specified access policy.
*
* @param capacity the capacity of this queue
* @param fair if {@code true} then queue accesses for threads blocked
* on insertion or removal, are processed in FIFO order;
* if {@code false} the access order is unspecified.
* @throws IllegalArgumentException if {@code capacity < 1}
*/
public ArrayBlockingQueue(int capacity, boolean fair) {
if (capacity <= 0)
throw new IllegalArgumentException();
this.items = new Object[capacity];
lock = new ReentrantLock(fair);
notEmpty = lock.newCondition();
notFull = lock.newCondition();
}
/**
* Creates a {@code LinkedBlockingDeque} with the given (fixed) capacity and
* the caller's {@link ReentrantLock} object.
* <p>
* <strong>Caution:</strong> By using the caller's lock, this constructor
* allows the caller to break the encapsulation of the synchronization and
* lock-based notification (signals). This can be used advantageously to
* create designs where an outer lock is shared by the collection which
* avoid deadlock arising from blocking operations on an inner lock while
* holding a distinct outer lock. However, the caller's decisions about its
* lock are no longer independent of the design decisions within this class
* since they share the same lock.
*
* @param capacity
* the capacity of this deque
* @param lock
* the lock object.
* @throws IllegalArgumentException
* if {@code capacity} is less than 1
*/
public LinkedBlockingDeque(final int capacity, final ReentrantLock lock) {
if (capacity <= 0) throw new IllegalArgumentException();
if (lock == null) throw new NullPointerException();
this.capacity = capacity;
this.lock = lock;
this.notEmpty = lock.newCondition();
this.notFull = lock.newCondition();
}
/**
* Creates an {@code ArrayBlockingQueue} with the given (fixed)
* capacity and the specified access policy.
*
* @param capacity the capacity of this queue
* @param fair if {@code true} then queue accesses for threads blocked
* on insertion or removal, are processed in FIFO order;
* if {@code false} the access order is unspecified.
* @throws IllegalArgumentException if {@code capacity < 1}
*/
public ArrayBlockingQueue(int capacity, boolean fair) {
if (capacity <= 0)
throw new IllegalArgumentException();
this.items = new Object[capacity];
lock = new ReentrantLock(fair);
notEmpty = lock.newCondition();
notFull = lock.newCondition();
}
public WifiSearcher( Context context, SearchWifiListener listener ) {
mContext = context;
mSearchWifiListener = listener;
mLock = new ReentrantLock();
mCondition = mLock.newCondition();
mWifiManager=(WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);
mWifiReceiver = new WiFiScanReceiver();
}