[jcifs] Max number of threads reached

Michael B Allen miallen at ioplex.com
Mon Sep 10 16:34:03 GMT 2007

On Mon, 10 Sep 2007 15:01:29 +0200
cesar at clazaro1.jazztel.es wrote:

> The code is very simple, as it just either reads all the URLs
> a directory contains or gets back a simple file.

Hi Cesar,

Thats the problem. You're creating a transport for each hostname in
the list. Because Java doesn't have adequate multiplexing facilities
we have to create a thread for each transport. Thus you're creating a
thread for each unique hostname in your list of URLs.

Do this: Sort the list of URLs first so that all hostnames are grouped
together. Reduce the soTimeout but make it 10 seconds (1 second runs the
risk of closing before a response has been received). Then add some logic
that detects when the hostname in the list has changed and add a short
configurable delay with a default of 500 milliseconds. If the hostname
does not change, the code can proceed as fast as it can read files because
the servername hasn't changed which means an existing transport will be
used (and thus you will not be creating new threads). When the hostname
does change, call Thread.sleep(500). Now the application will only be
abled to create at most 2 threads per second. That, coupled with the
natrual delay of network communication should slow things down enough
to keep the number of threads down.

You could also add a 'closeTransport' method to SmbFile that simply calls
transport.close() but note that you will still have to sort the list and
do everything described above minus the 500 ms delay. Otherwise, if you
simply close the transport after retrieving each URL, you would generate
a huge amount of traffic and the whole thing will become slow. Also a
network admin will probably come over and hit you over the head with a
rubber bat for conducting a DOS attack.


Michael B Allen
PHP Active Directory Kerberos SSO

More information about the jcifs mailing list