Making Your Application Thread-Safe

   

Many new concepts were covered in this chapter, from deadlocks to thread scheduling algorithms. Probably the most important concept to walk away from this chapter with, other than understanding the Thread API, is the concept of a critical section.

As stated earlier, a critical section is a part or multiple parts of an application that can be accessed simultaneously from multiple threads. If an application has more than a single thread running and there is shared data that can be accessed, this area has to be protected. By using the keyword synchronized, these critical sections can be made safe to allow multiple threads in an application to share the data. One of the most important things to remember when making your applications thread-safe is to look for any way that a thread has access to the shared data. Usually this is through the get and set methods for a member variable. However, there usually are other methods that make it possible for shared data to be accessed. You must either protect these as well, or remove them from public access.

Finally, remember that an entire method does not have to be synchronized in all cases. You have the ability to synchronize a block or section of code. This is sometimes more efficient and helps in preventing deadlock conditions.s

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net