[jcifs] Problem to write on SmbFile while trying to write by multithreading on java.

Michael B Allen ioplex at gmail.com
Thu Aug 2 11:35:35 MDT 2012

On Tue, Jul 31, 2012 at 7:36 AM, mrashid <rasheed.mamun at gmail.com> wrote:
> Hi All,
> I am facing some problems while writing on a particular SmbFile located at a
> shared location through SmbFileOutputStream.
> It seems pretty cool while doing the above job using single thread, but
> problem happens while attempting to write on that single SmbFile using
> multithread, some data is missing on SmbFile and the whole channel became
> slow more and more rather than single thread instead of being faster. I'm
> using different SmbFile objects to write on while using multithreading
> feature but nothing fruitful comes out.

Hi Rasheed,

You cannot operate on the same SmbFile object using multiple threads.
It is not thread safe. Each thread has to use a separate SmbFile

And note that you need to be careful about writing to the same file
with multiple threads (using JCIFS or anything else). Depending on the
flags used (append, shareAccess, etc), I don't know how the server
would handle that in each case. If you're doing something like
appending to a log file from multiple threads that should at least be
thread safe. But you should make sure that you write the entire log
entry with one call to write(). And you might need to open, write and
close the file for each write. Otherwise, if you want to keep the
OutputStream open for multiple writes, I would test that thoroughly
with a test program that used 10 threads to write hundreds of times
per second, let it run for 10 minutes and verify that it works and
that the file contains the expected data. I suspect it will work and
it should be very fast. Nothing should be "slow". If it does get
slower then something is serializing the writes.


Michael B Allen
Java Active Directory Integration

More information about the jCIFS mailing list