• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Interview question about synchronization in threads

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
My pie came with a little toothpic holding up this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic