File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DB Interface question

 
Chih-Wei Lee
Ranch Hand
Posts: 129
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My DB interface has something like

public long lock(int recNo) throws RecordNotFoundException;

If my Data.java implement above method like this

public synchronized long lock(int recNo) throws RecordNotFoundException {
.
//code
}

I add a synchronized keyword.
This is the only one lock method in my Data.java
Does this method is still valid and implements interface?

Thanks

Lee
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chih-Wei Lee:
My DB interface has something like

public long lock(int recNo) throws RecordNotFoundException;

If my Data.java implement above method like this

public synchronized long lock(int recNo) throws RecordNotFoundException {
.
//code
}

I add a synchronized keyword.
This is the only one lock method in my Data.java
Does this method is still valid and implements interface?

Thanks

Lee


I believe it could be ok, however, you might lose points on that. One of the goal in your locking mechanism (I think) is to synchronize as few lines as possible.

In your lock(), you might actually need to synchronize 2-3 lines while 3-4 other lines can afford to be run concurrently.

That's my opinion, I don't think you should do so, unless the interface forces you to.

Alex
 
Chih-Wei Lee
Ranch Hand
Posts: 129
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response.
So can I have the same capability with below code

public long lock(int recNo) throws RecordNotFoundException {
synchronized(this){
.
.

}
}

I don't quite understand the keyword "this". Does it mean the class itself or the method itself.

Thanks
 
Quintin Stephenson
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I've just signed up for my SCJD assignment today, so I don't know what the limitations are yet as I'm waiting for the emails to come through, so please excuse my ingorance.

Instead of using synchronize, why not use the lock interface. According to sun it provides a "more extensive locking operations than can be obtained using synchronized methods and statements. They allow more flexible structuring"

Cheers
Q
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chih-Wei Lee:
Thanks for your response.
So can I have the same capability with below code

public long lock(int recNo) throws RecordNotFoundException {
synchronized(this){
.
.

}
}

I don't quite understand the keyword "this". Does it mean the class itself or the method itself.

Thanks


synchronizing on this means the class.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic