[[threadpool]]
== ThreadPool

[NOTE]
======
This class is going to be deprecated. Do not use in new code.

The ThreadPool is used by several jPOS components, such as the ISOServer, and it was a 
good helper class 10 years ago. We will replace it by components of the Java Executors 
Framework at some point.
======


*org.jpos.util.ThreadPool*, takes care of managing a pool of threads. 

Its constructor looks like this: 

[source,java]
----

    public ThreadPool (int initialPoolSize, int maxPoolSize)
  
----

(See  link:http://jpos.org/doc/javadoc/org/jpos/util/ThreadPool.html[javadocs] for details). 

It's very useful to process short-lived threads, such as processing an
authorization transaction. Instead of creating a new thread per transaction,
you can create a ThreadPool at initialization time and then call its
+execute(Runnable r)+ method. 


The thread will be returned to the pool when your +run()+ method ends, so it is
not a good idea to have long-running threads (e.g., a +for (;;) { ... }+ loop) in
your Runnable. 

There's an inner interface called ThreadPool.Supervised that your Runnable can
optionally implement: 

[source,java]
----

    public class ThreadPool {
        public interface Supervised {
            public boolean expired ();
        }
    }
  
----

In this case, ThreadPool will call your +expired()+ method, and - if true -
will attempt to interrupt the expired thread. Note that while this does not
guarantee that your thread will gracefully end, it gives you a chance to get
out of a possible problem.  

[TIP]
=====
You can write some 'self-healing' code in your +expired()+ implementation, but
please make sure your code won't block for too long. Use only if you know
what you're doing. 
=====

ThreadPool implements ThreadPoolMBean, which exposes the following read-only
properties: 

[source,java]
----

    public int getJobCount ();
    public int getPoolSize ();
    public int getMaxPoolSize ();
    public int getIdleCount();
    public int getPendingCount ();
  
----

