jQuery in Action, 2nd edition*
The moose likes Threads and Synchronization and the fly likes Lock on the Object creates the Problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Lock on the Object creates the Problem." Watch "Lock on the Object creates the Problem." New topic
Author

Lock on the Object creates the Problem.

shashikant kale
Greenhorn

Joined: Aug 02, 2002
Posts: 6
Hello friends,
in that i had one problem :
Let say there are 2 thread ReaderThread and WriterThread.and one vector which had some values.
readerth reads from Vector and WriterTh writes into vector .
before reading from vec. i put lock onto vect. and then i read the data from it.so taht no other will access this vect.and after read'g is ok i'll realeas the lock from vector.

code: for Lock is here:

but it wont proplerly. so can u pl. suggest somethins reg. to locking on to the Vector which will be shared by my 2 threads.

thanks in advance,
shashi
[Added code[/code] tags - Jim]
[ August 02, 2002: Message edited by: Jim Yingst ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi shashjava,
First of all you probably need to change your display name per the naming policy at javaranch.
Now for your problem, you may want to try something like this instead:

Your read thread then calls lock, reads the vector and then calls unlock. Your write thread simply calls checkLock() which blocks until the lock is realeased.
Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Lock on the Object creates the Problem.
 
Similar Threads
wait and notify
Clarification in wait, notify and notifyAll methods
A few questions on wait(), sleep(int) and InterruptedException
Thread wait() and notifyall()
Doubt in one question