This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes thread newbie Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "thread newbie" Watch "thread newbie" New topic
Author

thread newbie

Luke Shannon
Ranch Hand

Joined: Sep 30, 2004
Posts: 239
hi all;

need some help. i have to create an object, one of the arguments in the constructor is a directory. however the directory must be "unlocked". there is another object in the program that holds the lock on the file.

basically i want to say, "before you create this object wait for the lock to be released on the folder". here is what i am trying:

//create the writer
System.out.println("Creating a new writer.");
System.out.println("The index is locked: " + IndexReader.isLocked(indexFileLocation));
while (IndexReader.isLocked(indexFileLocation)) {
try {
System.out.println("Waiting for the reader to release the lock on the index");
Thread.sleep(10000);
} catch (InterruptedException e2) {
System.out.println("INDEX ERROR: There was a problem waiting for the lock to release. " + e2.getMessage());
}
}

do i need a notify somewhere? will this sleep forever?

thanks,

luke
[ November 12, 2004: Message edited by: Luke Shannon ]

Luke
SCJP
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi Luke,

It seems you need Locking and not much of a threading. Locking would be required as your object/direcotry will be accessed by multiple threads.

Currently I am unable to throw more ideas on here but I would do that soon.

Meanwhile, you can google on how to locking in Java and think more while anybody else can give you more details about locking and all..

Regards
Maulin
Luke Shannon
Ranch Hand

Joined: Sep 30, 2004
Posts: 239
Thanks. After a quick google search I ended up using a Thread.sleep(), seems to do the trick.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

consider investigating Object.notify()
 
Don't get me started about those stupid light bulbs.
 
subject: thread newbie
 
Similar Threads
thread question in k&b's book for 1.5
My Locking...Advice needed...
The wai() method
Synchronization