• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get synchronous behavior from a non synchronized class

 
Pankaj Kumarkk
Ranch Hand
Posts: 112
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Clapham
Sheriff
Posts: 21867
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
satya anvesh
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
 
Madhan Sundararajan Devaki
Ranch Hand
Posts: 312
Java MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may try the following.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!