• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm also not sure that "++" and "--" are guaranteed to be atomic operations.

Moving to Threads and Synchronization...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
You didn't tell me he was so big. Unlike this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic