This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Problem in File lock using Threads in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Problem in File lock using Threads in java" Watch "Problem in File lock using Threads in java" New topic
Author

Problem in File lock using Threads in java

Pandian Ranganathan
Greenhorn

Joined: Oct 20, 2009
Posts: 13
Hi,

i am developing file lock concept in java. i am using two threads. first thread is in running state...second thread will wait untill first thread release... i cant make second thread will wait.. i am getting some exception...




i am getting exception like this

Inside Run...
fileC:\Test\Response File.txt
true
Channel sun.nio.ch.FileChannelImpl@addbf1
inside second try.....
inside second try----->sun.nio.ch.FileLockImpl[0:9223372036854775807 exclusive valid]
Inside finally.....
Inside Run...
fileC:\Test\Response File.txt
true
Channel sun.nio.ch.FileChannelImpl@190d11
inside second try.....
Exception java.nio.channels.OverlappingFileLockException
Inside finally.....
java.nio.channels.OverlappingFileLockException
at sun.nio.ch.FileChannelImpl$SharedFileLockTable.checkList(Unknown Source)
at sun.nio.ch.FileChannelImpl$SharedFileLockTable.add(Unknown Source)
at sun.nio.ch.FileChannelImpl.tryLock(Unknown Source)
at java.nio.channels.FileChannel.tryLock(Unknown Source)
at FileTest.run(FileTest.java:26)
at FileTest.main(FileTest.java:53)

Thanks in Advance...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42050
    
  64
Both threads need to synchronize on the same object, not different objects.


Ping & DNS - my free Android networking tools app
Pandian Ranganathan
Greenhorn

Joined: Oct 20, 2009
Posts: 13
Hi Ulf Dittmer,

Thanks For your response....

i am using single object for synchronize the both threads.... can you explain clearly...can you modify the code....


Thanks in Advance...

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40


I don't see any threading going on here -- meaning I don't see any threads being created.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40


It also looks like you never released the file lock too.

Henry
Sergey Babkin
author
Ranch Hand

Joined: Apr 05, 2010
Posts: 50
You do a tryLock() which throws if it can't obtain the lock immediately. Then in finally block you (commented out) attempt to release the lock even though you haven't obtained it (which is wrong, so commenting-out helps here). But if you succeed, then you end up not releasing the lock because thah line is commented out. You need to releas eteh lock only if tryLock() succeded.

Use lock() instead of tryLock(). Then the second thread would wait until the first thread releases the lock.

If you want to use tryLock(), then don't release it if you get the exception from tryLock() and release it if you don't get the exception.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

Be advised that file locks are useless for threading purposes. They only work between different programs.
Pandian Ranganathan
Greenhorn

Joined: Oct 20, 2009
Posts: 13
Hi All,

Thanks for your response...

Database stores internal and external results. The Internal results are stored against the one input and the External results are stored against the another input. So, i have to call the Stored procedure twice. Once with the one input and again with the another input to get internal and external results respectively.Those need to be accessed using sinle thread...if one use the thread other should wait unitl it finishes, second thread should check every 5 seconds whether first finishes .......... please provide sample code if possible.........

what concept i have to use other than synchronization......... give your suggestion... many thanks in advance
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42050
    
  64
For starters, as Henry pointed out, the code does not create threads. You probably meant to do new Thread(t).start() instead of t.run().

Then -in order to achieve synchronization- all threads need to use the same object as the lock; for example, FileTest.class, not any local variable within the object.

Working through the concurrency chapter of the Java Tutorial should be helpful: http://download.oracle.com/javase/tutorial/essential/concurrency/index.html
 
GeeCON Prague 2014
 
subject: Problem in File lock using Threads in java