Code example for Selector

Methods: wakeup

0
		adapter.registering();
 
		acquireSelectorGuard(); 
 
		try { 
			SelectionKey key = channel.register (selector, SelectionKey.OP_READ, adapter);
 
			adapter.setKey (key);
			adapter.registered();
 
			return adapter;
		} finally { 
			releaseSelectorGuard(); 
		} 
	} 
 
	public void unregisterChannel (ChannelFacade token)
	{ 
		if ( ! (token instanceof HandlerAdapter)) {
			throw new IllegalArgumentException ("Not a valid registration token");
		} 
 
		HandlerAdapter adapter = (HandlerAdapter) token;
		SelectionKey selectionKey = adapter.key();
 
		acquireSelectorGuard(); 
 
		try { 
			adapter.unregistering();
			selectionKey.cancel();
		} finally { 
			releaseSelectorGuard(); 
		} 
 
		adapter.unregistered();
	} 
 
	// ------------------------------------------------ 
	// package-local called from HandlerAdapter 
 
	// Place the given HandlerAdapter instance on the status change queue. 
	// The loop and nested try/catch blocks are to properly handle the 
	// InterruptedException that might be thrown when adding to the 
	// completion queue.  That exception is unlikely to ever happen here, 
	// but this is the proper code to handle the general case. 
	void enqueueStatusChange (HandlerAdapter adapter)
	{ 
		boolean interrupted = false;
 
		try { 
			while (true) { 
				try { 
					statusChangeQueue.put (adapter);
					selector.wakeup();
					return; 
				} catch (InterruptedException e) {
					interrupted = true;
				} 
			} 
		} finally { 
			if (interrupted) Thread.currentThread().interrupt();
		} 
	} 
 
	// ------------------------------------------------ 
	// private methods that always run in the selection 
	// thread, and hence do not need the selectorGuard. 
 
	private void invokeHandler (HandlerAdapter adapter)
	{ 
		adapter.prepareToRun();
		adapter.key().interestOps (0);
 
		executor.execute (new HandlerFutureTask (adapter));
	} 
 
	private void checkStatusChangeQueue() 
	{ 
		HandlerAdapter adapter;
 
		while ((adapter = statusChangeQueue.poll()) != null) {
			if (adapter.isDead()) {
				unregisterChannel (adapter);
			} else { 
				resumeSelection (adapter);
			} 
		} 
	} 
 
	private void resumeSelection (HandlerAdapter adapter)
	{ 
		SelectionKey key = adapter.key();
 
		if (key.isValid()) key.interestOps (adapter.getInterestOps());
	} 
 
	/** 
	 * Called to acquire and then immediately release a write lock 
	 * on the selectorGuard object. This method is only called by 
	 * the selection thread and it has the effect of making that 
	 * thread wait until all read locks have been released. 
 	 */ 
	private void selectorGuardBarrier() 
	{ 
		selectorGuard.writeLock().lock();
		selectorGuard.writeLock().unlock();
	} 
 
	// -------------------------------------------------------- 
 
	// Reader lock acquire/release, called by non-selector threads 
 
	/** 
	 * Grab a read lock on the selectorGuard object.  A handler thread 
	 * calls this method when it wants to mutate the state of the 
	 * Selector.  It must call releaserSelectorGuard when it is finished, 
	 * because selection will not resume until all read locks have been 
	 * released. 
	 */ 
	private void acquireSelectorGuard() 
	{ 
		selectorGuard.readLock().lock();
		selector.wakeup();
	} 
 
	/** 
	 * Undo a previous call to acquireSelectorGuard to indicate that 
	 * the calling thread no longer needs access to the Selector object. 
Connect your IDE to all the code out there  Get Codota for Java