Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interview question about synchronization in threads

 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some of the questions of my interview,

1) Whats the difference between synchronized block and method level synchronization?
2)why method level synchronization is not preferred? how does it slows down the performance?

3)I dont know if this question is related to threads, i thought it might be achieved through threads,

If two persons at the same time are trying to buy ticket for the same seat in a Airplane? How to avoid this situation and not let the seat become blocked?

can this be achieved using Threads?
 
luri ron
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Whats the difference between synchronized block and method level synchronization?

synchronized block is to acquire the monitor for the object/class you synchronized on.
syncrhonized method is to acquire the monitor of "this"

2)why method level synchronization is not preferred? how does it slows down the performance?

for performance purpose, you would only want to synchronized on the code block need to be synchronized. not the whole method.

3)I dont know if this question is related to threads, i thought it might be achieved through threads,

If two persons at the same time are trying to buy ticket for the same seat in a Airplane? How to avoid this situation and not let the seat become blocked?

can this be achieved using Threads?

synchronized would work in this case. as jdk 1.5/1.6, you could use CompareAndSet which is use less instructions but achieve the same effect as synchornized.
 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks luri,

can you please explain how to achieve question 3
the way you said
"synchronized would work in this case. as jdk 1.5/1.6, you could use CompareAndSet which is use less instructions but achieve the same effect as synchornized"
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anuradha Prasanna wrote:Thanks luri,
can you please explain how to achieve question 3


Look here for basics:
http://en.wikipedia.org/wiki/Compare-and-swap

The above method is implemented in Java in a set of AtomicXXXX classes, for exaple AtomicInteger:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/atomic/AtomicInteger.html

 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3)I dont know if this question is related to threads, i thought it might be achieved through threads,

If two persons at the same time are trying to buy ticket for the same seat in a Airplane? How to avoid this situation and not let the seat become blocked?


This is a problem of DB transactional concurrency control. It could be solved efficiently using optimistic concurrency control (by maintaining a version_id column in the DB table for Seats).
Doing it using in-memory synchronization would require synchronizing on some common object whenever the bookSeat() operation is invoked.
But that would have a huge performance impact as the bookSeat() would synchronize on the common object even when different seats are being booked.
 
amitabh mehra
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a problem of DB transactional concurrency control. It could be solved efficiently using optimistic concurrency control (by maintaining a version_id column in the DB table for Seats).


Ajay, can you please elaborate on this approach?
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@amitabh

Dude what's google there for ?

Anyway, check out http://en.wikipedia.org/wiki/Optimistic_concurrency_control and the references provided there.
 
amitabh mehra
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had already done that Ajay but i was looking for some application level code snippet / example to implement this
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic