Cisco Systems Interview Question
Country: India
Interview Type: In-Person
You could also have a LastModified column. Any thread before doing a write, checks this column - if the date it has is earlier than the table date, then the thread again has to read the data and re-do the modifications.
I gave the answer, Interviewer was not satisfied and said it is a database specific solution, give me java specific solution.
one thing that can be done is update all the threads of the database update using asynchronous event notifications and handling...generally large applications uses something like a MVC design...even there might not be a UI, you can use similar kind of architecture where the database interaction is done through a seperate Singeleton thread altogether.
If First thread is going to edit row data, First thread should take a read lock on the table row data, so that thread2 does not read a stale data which is going to be changed.
This shared data should be declared volatile, if Java is the language. If thread 2 has already read data before thread 1 making any change to it, thread 2 has read correct data at that time. After thread 1 has changed this data, this change will be visible to thread 1 too . A Volatile data means that all threads see the same data value and update by one thread to it should be visible to all the threads.
What about synchronized methods? "If an object is visible to more than one thread, all reads and writes to that object's variables are done through synchronized methods." : "h t t p://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html"
I think, Volatile can help. declare all the bean variable as volatile.
- a0125163 December 23, 2012