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 Interview question about synchronization in threads 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 "Interview question about synchronization in threads" Watch "Interview question about synchronization in threads" New topic
Author

Interview question about synchronization in threads

Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
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?


SCJP 6.0 90%
luri ron
Ranch Hand

Joined: Dec 11, 2008
Posts: 87
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

Joined: Mar 09, 2006
Posts: 115
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

Joined: Jun 21, 2008
Posts: 423
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

Joined: Nov 13, 2006
Posts: 154
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

Joined: Dec 05, 2006
Posts: 98
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

Joined: Nov 13, 2006
Posts: 154
@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

Joined: Dec 05, 2006
Posts: 98
I had already done that Ajay but i was looking for some application level code snippet / example to implement this
 
Consider Paul's rocket mass heater.
 
subject: Interview question about synchronization in threads
 
Similar Threads
Thread
Use of Volatile keyword
synchronization
Is there any alternative for synchronization?
Regarding Threads!!