• Post Reply Bookmark Topic Watch Topic
  • New Topic

a question about the synchronization.

 
Olena Golub
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Java-Gurus!
I have some questions about the synchronization.
I have one class, say Database. This class makes some I/O action with the file (RandomAccessFile). I synchronize every method, that works with the RandomAccessFile instance.


In the another class, say MyClass I use this Database instance to write or read from the file.
Every thread becomes its own version of the MyClass instance, but they will be work with the
same instance of the Database. I make this instance private static.
This looks like:

I synchronize the database instance, to avoid that two or more threads will be work with the database concurrently.
For example the first thread wants to call the write method and put the file pointer at position 10. The second wants
to write some data at the position 20 of the file. To avoit this I synchronize the instance of the database in MyClass.


My question is, is this thread save?
Or is this nested synchronization, that I need to avoid it?

Will this the second implementation more correct:

Could you help the greenhorn in multi-thread/synchronization world?
What is the best way to implement it?
Thanks a lot for your help!

Regards,
Olena
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the Database class from your first example and the MyClass method from your second. By synchronizing access to the Database class, you are synchronizing access to the RandomAccessFile, and that's all you need. You could use the Database class from your second example, but it's more verbose and less clear.

Also, you shouldn't be calling notifyAll() unless other objects are calling wait(). If they're waiting on the synchronization lock, you don't need to notify them.
 
Olena Golub
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
thanks a lot for your help!
But if I use the Database from the first example and MyClass from the second, I will have following problem:
For example. ThreadA calles the method read. And the threadB calles concurrently the method write.

-ThreadA: calles the method read.
-ThreadA: In method read will be called file.seek(position1), the filepointer now at position1
-ThreadB: calles the method write
-ThreadB: In the method write will be called file.seek(position2), the file pointer now at position2
-ThreadA: reads the information. But the file pointer now at position2. And the wrong information will be read.

Am I right?

Or if I synchronize all methods in the Class, only one Thread in a time can call a method from this class? Am I right?
With notifyAll() - sorry, was my mistake.
[ April 24, 2005: Message edited by: Olena Golub ]
 
Olena Golub
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand it!
if one class has some synchronize methods, and if the first thread calles one of the synchronize method, say method1, if the another thread wants to call the method2, it will wait until the first thread finishes the method1.
with

the whole class instance will be locked.

Am I right?
Thanks a lot for your help!!!
Olena
[ April 24, 2005: Message edited by: Olena Golub ]
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are understanding it correctly. Synchronizing an instance (non-static) method forces the calling thread to first obtain the lock on the object instance (not the entire class) before entering the method. It will wait if another thread holds the lock.

The MyClass from your first example was duplicating the synchronization by synchronizing on the Database. It would work as you coded it, but it's unnecessary and makes the code harder to understand. Note that a single thread can obtain multiple locks on the same instance, and all must be released for other threads to gain control of the lock.
 
Olena Golub
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear David,

thanks a lot for your help!
Regards,
Olena
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!