This week's book giveaway is in the Agile forum.
We're giving away four copies of Fixing your Scrum and have Ryan Ripley & Todd Miller on-line!
See this thread for details.
Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

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
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic