Create some Jaxb2Marshaller (say five), put them in the pool (use LinkedBlockingQueue ). When you create a thread, pass it a queue.
When a thread needs one, take() one of the queue / pool. When the pool is empty, threads will block on this call.
When a thread executes with Jaxb2Marshaller , put() , it returns to the queue, so other threads can use it.
If you find that thread blocks too often expect a marshaller, add more to the queue (see first step). This way you can easily configure the pool (or even configure it). Then the queue will automatically distribute them.
source share