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 7625011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:08:07+00:00 2026-05-31T05:08:07+00:00

I have read that you should use volatile when a thread accesses a variable

  • 0

I have read that you should use volatile when a thread accesses a variable to make sure that the threads see the correct value, but does that also apply when the variable is used within a method?

Example code:

public class Limiter
{
    int max = 0;

    public synchronized void doSomething()
    {
         max++;
         if(max < 10)
             System.out.println("Work");

         System.out.println(max);
    }

}

Is it safe to assume that if a multiple thread calls doSomething, then max will be set to the same state as when the previous Thread called the method?

Because doSomething() is synchronized, I know that only a single thread can modify max, but what happens when the next thread calls it? Can max be a different value because it doesn’t use volatile? Or is it safe because the “Limiter” instance modifies it itself?

  • 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-31T05:08:09+00:00Added an answer on May 31, 2026 at 5:08 am

    volatile is part of the declaration of a field, not part of its use. It would make no sense to declare a local variable as volatile, as local variables won’t be seen by different threads.

    In your case, you’re fine so long as no code in non-synchronized methods accesses max – the memory model basically makes sure that so long as all code acquires/releases the same monitor “guarding” a variable, all threads will see a consistent sequence of values. (Aside from anything else, the fact that the threads each have to acquire the monitor before accessing the value means only one thread will be able to access the value at a time – you could write a total ordering of “thread X had the monitor at time t0-t1, thread Y had the monitor at time t4-t5” etc.)

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

Sidebar

Related Questions

I have read that after installing some Eclipse plugins manually, one should use the
I have read that you should not use get json for retrieving sensitive data
Im writing a program that should read input via stdin, so I have the
I have read in many places that network connection in a j2me app should
I have read When to use 'volatile' in Java? but I'm still confused. How
I have read that after select we use column-names but I have found a
I have read that its bad idea to use platform default character encoding for
I have read that using database keys in a URL is a bad thing
I have read that while plug-ins are not supported for SQL Server Management Studio,
I have read that private variables in a base class are technically inherited by

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.