Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6535795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:24:54+00:00 2026-05-25T10:24:54+00:00

@ThreadSafe public class SynchronizedInteger { @GuardedBy(this) private int value; public synchronized int get() {

  • 0
@ThreadSafe
public class SynchronizedInteger {
    @GuardedBy("this") private int value;
    public synchronized int get() { return value; }
    public synchronized void set(int value) { this.value = value; }
}

The book says:

A good way to think about volatile variables is to imagine that they behave roughly like the SynchronizedInteger class
in Listing 3.3, replacing reads and writes of the volatile variable with calls to get and set.
…
This analogy is not exact; the memory visibility effects of SynchronizedInteger are actually slightly stronger than those of volatile variables. See
Chapter 16.

I checked chapter 16, but didn’t find an exact answer – how exactly the memory visibility guarantees are stronger?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:24:55+00:00Added an answer on May 25, 2026 at 10:24 am

    The main difference is that volatile creates a happens-before relationship between a write to the volatile variable and a subsequest read from it, whereas synchronized creates happens-before relationships between an unlock and a subsequent lock.

    So, in the case of SynchronizedInteger happens-before relationship is created between any consequent operations with SychrozniedInteger (i.e. get() and set() calls), and it doesn’t matter whether they are reads or writes. It differs from the behaviour of volatile int that provide happens-before only between a write and a consequent read.

    Actually I can’t imagine any meaningful example to illustrate this difference, so that these behaviours really differ only slightly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class ThreadSafe implements ITaskCompletionListener { private final Set<String> taskIds = new HashSet<String>(); private
In other words, is this Singleton implementation thread safe: public class Singleton { private
based on this example : @Service public class Purchase { @PersistenceContext private EntityManager em;
Take this code: public class MyClass { private final Object _lock = new Object();
Is this ValueStore class threadsafe? Does the lock scope in GetInt(string key) need to
Consider the following code: public class Foo { private static final Object LOCK =
I have this question. public class Foo : object { public override bool Equals(obj
Suppose I have the following class: public class Foo { private List<Integer> list =
Next is a simple semaphore implementation. public class Semaphore { private boolean signal =
Hi Is the class below threadsafe? class ImmutablePossiblyThreadsafeClass<K, V> { private final Map<K, V>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.