permaculture playing cards*
The moose likes Threads and Synchronization and the fly likes Why synchronization here? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Why synchronization here?" Watch "Why synchronization here?" New topic
Author

Why synchronization here?

Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 430
I'm reading Concurrent Programming in Java, and getting confused about the fist example.

After looking through the code. I don't find any possibilities that multi-threads invoke methods or get/update attributes on the same object. Why synchronization is added? Why add synchronization to Applet's start() and stop() methods? Would you help me to clarify this? Thanks.


To be or not to be. It's a question.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The move and draw methods of the particles need to be synchronized because they are working on the same data, and are called from different threads - move from the thread of the particle, draw from the AWT event dispatch thread.

For the other synchronized code, I'm as confused as you are...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why synchronization here?
 
Similar Threads
Null pointer exception thrown in paintComponent
Previous image does not clear on repainting Canvas in a loop with different positions
CHEECKY but i need help FAST please!
How to turn an Applet into a Standalone?
synchronization confusion