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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:30:15+00:00 2026-05-26T21:30:15+00:00

I didn’t think threading would be this difficult sigh . Anyways, the only way

  • 0

I didn’t think threading would be this difficult sigh.
Anyways, the only way I could think of executing a function after a thread has completed is to use a static counter to increment whenever a thread ran.

if(++threadcounter==3){doSomething(); threadcounter =0;}

I found this wasn’t a good idea because the threadcounter at times never reaches 4.

So I used atomic integer

if(atomicint.incrementAndGet()==4){doSomething(); atomicint.set(0);}

The counter is 5 or 0 and the app freezes. I don’t know what’s happening. How to use a correct counter?
Thanks

EDIT:

  • 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-26T21:30:16+00:00Added an answer on May 26, 2026 at 9:30 pm

    The simplest way to tackle this is with a good old-fashioned lock:

    boolean shouldDoSomething;
    synchronized {
        ++threadCounter;
        if (threadCounter == 4) {
            threadCounter = 0;
            shouldDoSomething = true;
        }
        else {
            shouldDoSomething = false;
        }
    }
    if (shouldDoSomething) doSomething();
    

    This will create contention on the lock, but over a very, very brief piece of code – a load, a store, and a few arithmetic instructions.

    Your use of AtomicInteger is wrong, because there is no locking or other concurrency control linking the incrementAndGet and the set, which means there is a potential race condition (value is 3, thread A increments to 4, thread B increments to 5, thread A sets to 0).

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

Sidebar

Related Questions

I didn't design this table, and I would redesign it if I could, but
I didn't think this would be as tricky as it turned out to be,
I didn't find a way to do this - only to edit the translations
I didn't find a better way to phrase this question in the title. If
I didn't know methods could have floating blocks like this: class X { public
I didn't get the answer to this anywhere. What is the runtime complexity of
I didn't see any similar questions asked on this topic, and I had to
Didn't know how else to put this. Say I have a JavaScript method that
I didn't think it was possible for client side applications to access the harddrive?
I didn't find any info on searches when I looked this up. I've been

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.