This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Synchronize on int shouldn't be necessairy, should it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronize on int shouldn Watch "Synchronize on int shouldn New topic
Author

Synchronize on int shouldn't be necessairy, should it?

Andre Timmer
Greenhorn

Joined: Apr 02, 2003
Posts: 1
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

Briefly: each thread can have its own memory cache of the shared variable. That cache may only be updated at a "memory barrier", which synchronization provides. So whenever two threads will both use the value of a single variable, access to that variable should be synchronized. Otherwise each thread may work on its own "local copy" of the shared variable.

Note also that stop() is deprecated for a reason: it's dangerous. In particular, it doesn't guarantee that those caches will be flushed; changes made to the shared variable may thus never take effect.


[Jess in Action][AskingGoodQuestions]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I'm also not sure that "++" and "--" are guaranteed to be atomic operations.

Moving to Threads and Synchronization...


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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

Originally posted by Ernest Friedman-Hill:

Note also that stop() is deprecated for a reason: it's dangerous. In particular, it doesn't guarantee that those caches will be flushed; changes made to the shared variable may thus never take effect.


stop() accomplishes the thread stop by forcing an unrolling of the stack via a ThreadDeath object. Unfortunately, if the thread is not ready for it, (and you can read that to mean anything you want) two dangerous things can happen. Synchronization locks can be freed at locations where it is not acceptable. Data variables may be left in a state that is not expected to happen, due to an unexpected exit.

This can lead to data corruption, unexpected states / conditions, deadlock, etc.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

Originally posted by Ilja Preuss:
I'm also not sure that "++" and "--" are guaranteed to be atomic operations.


"++" is syntactic sugar for a load, increment, and store. It is *not* atomic.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Synchronize on int shouldn't be necessairy, should it?