Chapter 8: Cooperative Synchronization

 < Day Day Up > 



8.1 Introduction

Synchronization is used to coordinate asynchronous activities, such as threads. Up to this point in the book, synchronization of asynchronous activities has always been on shared resources; this is called competitive synchronization, because the activities are always competing for the shared resource. However, at times activities must synchronize on an event that must occur before all threads can continue; this type of synchronization is called cooperative synchronization. This chapter explains what cooperative synchronization is and why it is important, as well as some of the pitfalls that can occur when implementing it.

The animator presented in Chapter 7 is used to demonstrate visually cooperative synchronization. All of the examples using the animator in Chapter 7 had objects that were created in the main thread, and then the animator simply called draw methods in these objects. The objects did not run in threads themselves but allowed their draw methods to be called in the GUI thread; therefore, they could only implement actions in the GUI thread and could not run independently from the GUI thread. In the examples in this chapter, the objects that are animated will implement threads, which requires that the threads cooperate with the GUI thread to implement the correct behavior.

A number of the same problems encountered in competitive synchronization will also be present in cooperative synchronization. Too little synchronization will result in race conditions and activities that do not cooperate correctly, while too much synchronization will result in activities that either deadlock or have performance problems. As always, care must be taken to implement an adequate amount of synchronization to solve the problem safely, but not so much that it causes liveness concerns.



 < Day Day Up > 



Creating Components. Object Oriented, Concurrent, and Distributed Computing in Java
The .NET Developers Guide to Directory Services Programming
ISBN: 849314992
EAN: 2147483647
Year: 2003
Pages: 162

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