Re: Multithreaded file locking solution?

From: Joseph (someone_at_arbitrary.org)
Date: 10/10/03


Date: 10 Oct 2003 14:01:13 -0700

Hi Giri,

No exceptions get thrown by the tryLock statement.

What if it is a different Java process? That would not be considered
the same virtual machine, right?

$ java myProg &
$ java myProg &

And sit and watch what happens.

First I listFiles() in a directory, then with each file I find I
create a RandomAccessFile then a FileChannel and then do tryLock on
the channel. If the lock is null, I continue to the next file. If it
is not null, I rename the file (moving it to a spool dir) and unlock
it. I then create an instance of my parser class passing among other
things the newly renamed File to its constructor then start() it.

Nothing bad happens until I see that sometimes the same file gets
passed by each process to 2 new parser threads. And then of course
problems occur when thet both try to do things to the file.

The programs continue to function as they are supposed to, but the
whole point of this is to be able to increase capacity, and having
processes falling overthemselves isn't really a solution.

Any suggestions?

Thanks,

Joseph

Giridhar Pendyala <pendyala@india.hp.com> wrote in message news:<3F853FE6.7000600@india.hp.com>...
> Hi Joseph,
>
> tryLock throws OverlappingFileLockException - If a lock that overlaps
> the requested region is already held by this Java virtual machine, or if
> another thread is already blocked in this method and is attempting to
> lock an overlapping region.
>
> Are you not getting this exception?
>
> regds,
> Giri
>
> Joseph wrote:
> > Hello All,
> >
> > I have a class which establishes a JMS network connection, and then
> > sniffs a directory for new files. every time it sniffs a new file, it
> > trylocks it (to make sure the Python program that created the file is
> > finsished with it) then starts an instance of a runnable class which
> > parses the passed file and sends some stuff out the passed JMS
> > connection.
> >
> > This works just fine, but I would like to now have multiple instances
> > of my sniffer/connection class because I want multiple JMS
> > connections. But it trips up on the file locking because the file lock
> > sees it all as one process. Ideally if I had two classes sniffing the
> > same directory, both would try to lock the file and one would succeed,
> > and the other would not and would ignore it and grab the next file.
> > But both are able to get the lock because the lock makes no
> > distinction between the processes and all sorts of bad things happen.
> >
> > So what should I do? I could make the snifferclass open several
> > connections and have it assigns them round robin style to the parse
> > threads, but that would involve changing a bunch of things (and
> > potentially breaking things). The point of all of this is to be able
> > to open more connections when there is heavy traffic. And the easiest
> > way to be able to do that would be to start multiple insrtances of my
> > existing program when needed.
> >
> > Any suggestions?
> >
> > Thanks,
> >
> > Joseph



Relevant Pages

  • Re: Why "lock" functionality is introduced for all the objects?
    ... very nature of objects in Java. ... synchronization, well, simple. ... can use it as a lock (strictly speaking, a Hoare monitor). ...
    (comp.lang.java.programmer)
  • Re: SQL Server 2005 high lock requests/second and CPU after application upgrade
    ... So what did you mean by 'upgrade' and why does that have the effect you see? ... Of course we know nothing about the architecture from your post so anything I say from here is speculation but the odd point is the number of connections. ... The application is possibly the most annoying app I've ever had the ... upgrade our CPU usage and lock requests per second have really shot ...
    (microsoft.public.sqlserver)
  • Re: Thread Management
    ... You can synchronise on an object to enforce a critical section. ... It is a concern though that you're holding a lock during a wait. ... If load testing is what you're after, then you'd want other threads to ... For handing results to other threads, Java 1.4 provides a between-thread ...
    (comp.lang.java.programmer)
  • Re: SyncLock (Lock)
    ... sometime you may have finer grain locking going on. ... is nothing wrong with locking on your list object if that is the only lock ... | I come from a Java background and I'm familiar with monitors in java. ...
    (microsoft.public.dotnet.framework)
  • Re: Parallelization on muli-CPU hardware?
    ... A GIL is a killer. ... > What the GIL buys us is no lock contention and no locking overhead ... course granularity, and badly with finely parallel algorithms. ... For example, Java strictly specified what ...
    (comp.lang.python)