• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get synchronous behavior from a non synchronized class

 
Ranch Hand
Posts: 112
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was asked in a interview following
There is a class Customer which is not synchronized. How do you access it in a synchronized manner. The constraint is that you do do not have access to java code of Customer class.

I answered: I will create a wrapper class using decorator pattern and expose synchronized version of all the methods of Customer class.

e.g



I told that I will ask the developer to use this class in a polymorpic manner instead of Customer class.

The interviewer said I can simply achieve it by putting it in a synchronized collection.
I think the solution of using synchronized collection will not work because the collection is only thread safe and not the object that collection contains(ie Customer object)

My question:
is my reasoning correct? If not can anybody help me understand how interviewer's solution will work.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have reproduced the question accurately, then what you said the interviewer said is (to be polite) incorrect.

As for your idea, it's certainly better than the interviewer's idea. It may or may not work in real life -- for example to make it work, you would have to prevent people from accessing Customer objects directly.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:If you have reproduced the question accurately, then what you said the interviewer said is (to be polite) incorrect.

As for your idea, it's certainly better than the interviewer's idea. It may or may not work in real life -- for example to make it work, you would have to prevent people from accessing Customer objects directly.



Hi Paul, can you provide the answer for that question or is there a solution for that? Thanks
 
Ranch Hand
Posts: 312
MS IE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may try the following.
reply
    Bookmark Topic Watch Topic
  • New Topic