posted 16 years ago
Hi Fei, synchronizing the entire method, while technically could work, you need to take into consideration performance. Imagine a scenario where many threads are vying for access to the methods of your object. Each of them having to wait for the entire method to execute before they could possibly have their turn, would invariably result in poor performance. The best approach with multi-threaded programming is to synchronize only the resources that's state can be affected by multiple threads. This could be the RandomAccessFile object in your data class, or some sort of map object in a locking class. This approach does require you to think carefully about your design, but the improvements in performance are well worth it. Hope this helps.