View Javadoc

1   /*
2    * Copyright 2000 Computer System Services, Inc.
3    *
4    * Permission to use this software for any purpose is granted provided that
5    * this copyright notice is preserved.
6    *
7    * This software is provided as-is and without warranty as to its
8    * fitness for any purpose.  In other words, Computer System Services,
9    * Inc. does not guarantee that this software works.  It is provided
10   * only in the hope that it may be found useful by someone.
11   *
12   * Please e-mail tttaylor@cssassociates.com if you find any errors
13   * or want to request changes/enhancements.
14   */
15  package twcsckernel.projectbase.rmi.socketfactory;
16  
17  import java.io.*;
18  import java.net.*;
19  import java.util.*;
20  
21  /***
22   * Provides a simple producer/consumer pool of sockets.
23   *
24   * @author Tim Taylor  -- tttaylor@cssassociates.com
25   */
26  public  class SocketPool {
27      private LinkedList<Socket> socketList = new LinkedList<Socket>();
28      
29      public synchronized Socket getSocket() throws InterruptedIOException {
30          try {
31              while (socketList.isEmpty()) {
32                  this.wait();
33              }
34          }
35          catch (InterruptedException e) {
36              throw new InterruptedIOException();
37          }
38          
39          return (Socket) socketList.removeFirst();
40      }
41      
42      public synchronized void addSocket(Socket socket) {
43          socketList.add(socket);
44          this.notifyAll();
45      }
46  }
47