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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:05:08+00:00 2026-06-15T01:05:08+00:00

While using multiple threads I have learnt to use Static variables whenever I want

  • 0

While using multiple threads I have learnt to use Static variables whenever I want to use a counter that will be accessed by multiple threads.

Example:

static int count=0; Then later in the program I use it as count++;.

Today I came across something called AtomicInteger and I also learned that it is Thread safe and could use one of its methods called getAndInrement() to achieve the same effect.

Could anyone help me to understand about using static atomicInteger versus static int count?

  • 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-06-15T01:05:09+00:00Added an answer on June 15, 2026 at 1:05 am

    – AtomicInteger is used to perform the atomic operation over an integer, its an alternative when you don’t want to use synchronized keyword.

    – Using a volatile on a Non-Atomic field will give inconsistent result.

    int volatile count;
    
    public void inc(){
    
    count++
    
    }
    

    – static will make a variable shared by all the instances of that class, But still it will produce an inconsistent result in multi-threading environment.

    So try these when you are in multithreading environment:

    1. Its always better to follow the Brian’s Rule:

    When ever we write a variable which is next to be read by another
    thread, or when we are reading a variable which is written just by
    another thread, it needs to be synchronized. The shared fields must be
    made private, making the read and write methods/atomic statements
    synchronized.

    2. Second option is using the Atomic Classes, like AtomicInteger, AtomicLong, AtomicReference, etc.

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

Sidebar

Related Questions

Suppose I have singleton class that acts as a data cache. Multiple threads will
I am using multiple threads in my application using while(true) loop and now i
I have a python app that uses multiple threads and I am curious about
I have a program that scales badly to multiple threads, although – theoretically –
I have a method to process the rows from a datatable using multiple threads,
While using the vector why do we sometime use the operator[] like homework[mid] but
I am currently programming a python-based datagram-Server using threads and all of that. I
I have an application running multiple threads. The threads do NOT share an ObjectContext
I have an application that is already using the Spring Framework and Spring JDBC
I have no doubt that for client applications, AsParallel() will bring some out-of-the-box performance

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.