private void readNextValue() { try { curValue = res.take(); } catch (InterruptedException interEx) { Thread.currentThread().interrupt(); } }
/** * After starting this class (start()), this method MAY be called to block the thread that created * the instance of this class. Otherwise, the thread will finish and the instance of this class * will be terminated. * * @throws InterruptedException unsuccessful join */ public void join() throws InterruptedException { // TODO: // initiate a graceful termination procedure (i.e., close()). log.info("joining"); @SuppressWarnings("unused") Object object = dispatcherJoin.take(); }
@Override public void run() { assertSame(this, Thread.currentThread()); try { while (true) { Request request = requestQueue.take(); Object result; try { result = invokeMethod(request.methodName, request.arguments); } catch (ThreadDeath death) { return; } catch (InvocationTargetException exception) { responseQueue.put(new Response(request.methodName, null, exception.getTargetException())); continue; } catch (Throwable throwable) { responseQueue.put(new Response(request.methodName, null, throwable)); continue; } responseQueue.put(new Response(request.methodName, result, null)); } } catch (ThreadDeath death) { return; } catch (InterruptedException ignored) { // SynchronousQueue sometimes throws InterruptedException while the threads are stopping. } catch (Throwable uncaught) { this.uncaughtThrowable = uncaught; } }
/** * Obtain a queue message synchronously. * * @param queueName Name of the queue from where to retrieve a message * @param timeout Timeout, in milliseconds. When timeout is reached a TimeoutException is thrown. * Zero means that the client wants to wait forever. A negative value means that the client * doesn't want to wait if there are no messages is local agent's queue. * @param reserveTime Message reserve time, in milliseconds. Polled messages are reserved, by * default, for 15 minutes. If clients prefer a different reserve time , bigger or small, they * can specify it. * @param acceptRequest An AcceptRequest object used handling Accept messages. * @return A notification containing the queue message. Or null if timeout was a negative value * and there was no message in local agent's queue. */ public NetNotification poll( String queueName, long timeout, long reserveTime, AcceptRequest acceptRequest) throws Throwable { if (StringUtils.isBlank(queueName)) throw new IllegalArgumentException("Mal-formed Poll request. queueName is blank."); NetPoll poll = new NetPoll(queueName, timeout); NetAction action = new NetAction(ActionType.POLL); action.setPollMessage(poll); NetMessage message = buildMessage(action); SynchronousQueue<NetMessage> synQueue = new SynchronousQueue<NetMessage>(); synchronized (_syncSubscriptions) { if (_syncSubscriptions.containsKey(queueName)) throw new IllegalArgumentException("Queue " + queueName + " has already a poll runnig."); _syncSubscriptions.put(queueName, message); pendingPolls.put(queueName, synQueue); } if (reserveTime > 0) { message.getHeaders().put(Headers.RESERVE_TIME, reserveTime + ""); } if (acceptRequest != null) { poll.setActionId(acceptRequest.getActionId()); PendingAcceptRequestsManager.addAcceptRequest(acceptRequest); } getNetHandler().sendMessage(message); NetMessage receivedMsg = synQueue.take(); synchronized (_syncSubscriptions) { _syncSubscriptions.remove(queueName); pendingPolls.remove(queueName); } if (receivedMsg == BrokerProtocolHandler.TimeoutUnblockNotification) throw new TimeoutException(); if (receivedMsg == BrokerProtocolHandler.NoMessageUnblockNotification) return null; NetNotification m = null; if (receivedMsg.getAction().getActionType().equals(ActionType.NOTIFICATION)) { m = receivedMsg.getAction().getNotificationMessage(); } else { log.error( "Poll unbloqued by a message that wasn't of any of the expeceted error nor a notification."); return null; } return m; }
// Causes the caller thread to block until an object is placed in the sync queue/ // If an Exception is place in the queue, it this thrown by the calling thread. private void join() throws Exception { while (true) { Object result = null; try { result = queue.take(); if (result instanceof Exception) { throw (Exception) result; } break; } catch (InterruptedException e) { if (result != null) throw e; } } }
public void run() { for (Course course : Course.values()) { Food food = course.randomSelection(); try { waitPerson.placeOrder(this, food); // Blocks until course has been delivered: print(this + "eating " + placeSetting.take()); } catch (InterruptedException e) { print(this + "waiting for " + course + " interrupted"); break; } } print(this + "finished meal, leaving"); }
public void waitForUiThread(Handler icHandler) { if (DEBUG) { ThreadUtils.assertOnThread(icHandler.getLooper().getThread(), AssertBehavior.THROW); Log.d( LOGTAG, "waitForUiThread() blocking on thread " + icHandler.getLooper().getThread().getName()); } try { Runnable runnable = null; do { runnable = mIcRunnableSync.take(); runnable.run(); } while (runnable != mIcSignalRunnable); } catch (InterruptedException e) { } }
@Override public Task awaitTask() throws InterruptedException { sem.release(); return queue.take(); }