AUTHENTICATING
Dont know if this is the place to post this.
I am using MINA for a xmpp lib i am creating. I have a list of listeners, to listen for connection changes. So there is a function:
public void nowConnected() {
synchronized (connectionListeners) {
for (ConnectionListener connList : connectionListeners)
connList.connected();
}
}
Now if i understand this correctly. The packetRecievedListener i have created is given a thread by MINA out of the pool. That thread then executes the nowConnected() so the program flow will look as follow.
Thread1 --> ConnectionListener1.connected()
--> ConnectionListener2.connected()
--> ConnectionListener3.connected()
Now say for instance i want to have in connectionListener1.connected() a authenticate connection function that requires a wait(block) for a result. This will stop the function call of connected for ConnectionListener 2 and 3 until ConnectionListener1 has completed.
Now how can i stop this form happening. I hope there is a way of doing this without creating more threads since this scenario will happen a lot and the pc does not have much memory.
Any help would be great. Even just a point into a direction.
Thanks,
Phil
[1281 byte] By [
PsyMonkeya] at [2007-11-26 17:45:33]

# 1
I can't see that you can prevent this. It seems to me (and I maybe wrong) that you have a single thread locking the ConnectionListeners list and performing a function on each listener in that list, and one of those functions requires a synchronous method call. The only ways around this are a) like you said, create more threads to execute the listener.connected() methods - which you have limited resources for, or b) make the authenticated method asynchronous (which in implementation would require another thread for a callback or something)
Sorry I can't be more help, but if I understand your intention correctly, it's threads.
# 4
This mite be a silly question but if I create a Executors (java.util.concurrent.Executors) with a thread pool of 5. And I never use this Executors in my program will there still be a thread running for the Executors that is never going to be used?
What i want to do is as follow. I want to use this pool of thread. Theses Threads will only wait for a reply from a server. Then they will kill them selfs. So the Executors will most of the time have nothing in them. Will they be using a thread?
Hope this is clear, if not i will try again on Monday. Enjoy the weekend!!
# 5
Not a silly question at all! The Executors class is a service to ease the burden creating an ExecutorService. You have two choices;
1) Use Executors.newCachedThreadPool()
which will only create threads as needed and reuse existing ones if available, but has no upper limit on the amount of threads it will create if it needs them. This would be the easiest option.
2) Use Executors.newCachedThreadPool(ThreadFactory)
where you supply an implementation of the ThreadFactory to supply threads as they are required, but impose an upper limit of 5. A bit more work but still not too bad.
Choice is yours.
# 6
As SimonKent1 indicated it depends on the how the pool is created.
Normally core pool threads are created on demand when tasks are submitted to the executor - unless you use preStartAllCoreThreads, which starts them all in advance.
Threads from core to max are created on demand and can terminate if idle by setting the idle timeout.
In Java 6 core threads can also timeout.
Generally if you create an Executor you need to manage its lifecycle, and invoke shutdown() when the pool is no longer needed.