-1

I have a "connection pool handler" which in its class has a array list of "Connection Handlers" I was wondering whether it is possible to have the "Connection Handlers" remove themselves from said list when they close the connections themselves and if so how?

public class ConnectionHandlerPool
{
private static final ArrayList<ConnectionThread> POOL = new ArrayList<ConnectionThread>;

<code to get stuff out of pool>

Connection thread class

public class ConnectionHandlerThread
{
<stuff that creates the connection>
<stuff that if exceptions occur i.e. a stream or socket closes it needs to close all the connections and remove from pool>
<that way of removing it from arraylist in the class above it>
Charlie Hardy
  • 175
  • 1
  • 3
  • 14

2 Answers2

2

The simplest way to achieve this doesn't require complicated listener design or even interfaces. Note: Following code does not take into account all other required logic (checking out, ensuring a connection can't be checked out twice, etc.).

public class ConnectionHandlerPool {
    private static final List<ConnectionThread> pool = new ArrayList<ConnectionThread>();
    // When new threads are created, they get a reference to the pool they're in
    private void addConnection() {
        pool.add(new ConnectionThread(pool));
    }
    private void disconnect(ConnectionThread ct) {
        pool.remove(ct);
    }
}

public class ConnectionHandlerThread {
    private final ConnectionHandlerPool pool;
    public ConnectionHandlerThread(ConnectionHandlerPool pool) {
        this.pool = pool;
    }

    private void destroy() {
        pool.disconnect(this);
    }
}
Kayaman
  • 72,141
  • 5
  • 83
  • 121
0

After a bit of tinkering and talking to some technical mates I have resolved it.

  1. Create a interface which has a method in it that takes a ConnectionHandlerThread
  2. Have the pool implement said interface and have said method remove the ConnectionHandlerThread removed from the arraylist.
  3. Have the connection handler pool class add itself as a listener to the ConnectionHandlerThread by creating a method called addListener
  4. Call addListener in the thread and have it pull through the interface
  5. Call the interface.method from the class when you want to remove it.
Charlie Hardy
  • 175
  • 1
  • 3
  • 14
  • Well that's one overly complicated way of doing it. The interface and listener functionality is unnecessary, since you could've basically done `handlerPool.removeConnection(this)`, provided that connections have a reference to a (single) pool. – Kayaman Aug 01 '17 at 14:29
  • @kayman the issue is java passes by value so I cant provide them with a reference to a single pool as when i go to `runhandlerPool.removeConnection(this)` it will only edit the version that is within itself and none of the others. – Charlie Hardy Aug 01 '17 at 14:54
  • No. Java passes everything by value yes, including references. It will work perfectly as I described, but you need to read this: https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Kayaman Aug 01 '17 at 16:28