• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Problem in File lock using Threads in java

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Rancher
Posts: 43027
76
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

Henry
 
author
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 13478
304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic