KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > kernel > ConnectionList


1 package com.ubermq.kernel;
2
3 import java.nio.channels.*;
4 import java.util.*;
5
6 /**
7  * A simple selector-aware queue of connections that are waiting to
8  * be serviced by an I/O thread.
9  */

10 public class ConnectionList {
11     private LinkedList list = new LinkedList();
12     private Selector selectorToNotify;
13     
14     /**
15      * Creates a ConnectionList. When a connection is added to the list
16      * the given selector's <code>wakeup</code> method is called.
17      */

18     public ConnectionList(Selector sel) {
19         this.selectorToNotify = sel;
20     }
21     
22     /**
23      * Adds a connection to the connection list for ultimate use
24      * by an I/O thread that is selecting on the selector specified at
25      * creation time.
26      */

27     public synchronized void push(IConnectionInfo newlyConnectedInfo) {
28         list.add(newlyConnectedInfo);
29         selectorToNotify.wakeup();
30         Thread.yield();
31     }
32     
33     /**
34      * Removes a connection from the list and provides it to the caller.
35      * This should be called by an I/O thread when its selector is woken up.
36      * @return an IConnectionInfo instance representing the new connection.
37      */

38     public synchronized Object JavaDoc removeFirst() {
39         if(list.size() == 0)
40             return null;
41         
42         return list.removeFirst();
43     }
44 }
45
46
Popular Tags