This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in File lock using Threads in java

 
Pandian Ranganathan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both threads need to synchronize on the same object, not different objects.
 
Pandian Ranganathan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Henry
 
Sergey Babkin
author
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6323
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be advised that file locks are useless for threading purposes. They only work between different programs.
 
Pandian Ranganathan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic