Apple Interview Question
Software Engineer / DevelopersThe semantics of synchronized do guarantee that only one thread has access to the protected section at one time, but they also include rules about the synchronizing thread's interaction with main memory. A good way to think about the Java Memory Model (JMM) is to assume that each thread is running on a separate processor, and while all processors access a common main memory space, each processor has its own cache that may not always be synchronized with main memory. In the absence of synchronization, it is allowable (according to the JMM) for two threads to see different values in the same memory location. When synchronizing on a monitor (lock), the JMM requires that this cache be invalidated immediately after the lock is acquired, and flushed (writing any modified memory locations back to main memory) before it is released. It's not hard to see why synchronization can have a significant effect on program performance; flushing the cache frequently can be expensive.
source: wwwdotibmdotcom/developerworks/library/j-threads1/index.html
This is so because in case of hash function we have a%n ==number value.For example consider a case where we have three numbers 143876%[total number of array] 143876%23 say column number 9 which gives it a single hash function. However if e consider the case of using array we will have to give a sequential function in index of array
a[0] =12 ,a[1] =13 etc...
In external syn we only synchronize the our atomic code(i.e critical section). but in classes like Vector all function are synchronized(even constructor, getter, setter,methods)
- Venkat January 20, 2011