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

  • Home
  • SEARCH
  • 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 8874489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:44:08+00:00 2026-06-14T18:44:08+00:00

I am working on a project to create a simple auction server that multiple

  • 0

I am working on a project to create a simple auction server that multiple clients connect to. The server class implements Runnable and so creates a new thread for each client that connects.
I am trying to have the current highest bid stored in a variable that can be seen by each client. I found answers saying to use AtomicInteger, but when I used it with methods such as atomicVariable.intValue() I got null pointer exception errors.

What ways can I manipulate the AtomicInteger without getting this error or is there an other way to have a shared variable that is relatively simple?

Any help would be appreciated, thanks.

Update

I have the AtomicInteger working. The problem is now that only the most recent client to connect to the server seems to be able to interact with it. The other client just sort of freeze.

Would I be correct in saying this is a problem with locking?

  • 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-14T18:44:09+00:00Added an answer on June 14, 2026 at 6:44 pm

    Well, most likely you forgot to initialize it:

    private final AtomicInteger highestBid = new AtomicInteger();
    

    However working with highestBid requires a great deal of knowledge to get it right without any locking. For example if you want to update it with new highest bid:

    public boolean saveIfHighest(int bid) {
        int currentBid = highestBid.get();
        while (currentBid < bid) {
            if (highestBid.compareAndSet(currentBid, bid)) {
                return true;
            }
            currentBid = highestBid.get();
        }
        return false;
    }
    

    or in a more compact way:

    for(int currentBid = highestBid.get(); currentBid < bid; currentBid = highestBid.get()) {
        if (highestBid.compareAndSet(currentBid, bid)) {
            return true;
        }
    }
    return false;
    

    You might wonder, why is it so hard? Image two threads (requests) biding at the same time. Current highest bid is 10. One is biding 11, another 12. Both threads compare current highestBid and realize they are bigger. Now the second thread happens to be first and update it to 12. Unfortunately the first request now steps in and revert it to 11 (because it already checked the condition).

    This is a typical race condition that you can avoid either by explicit synchronization or by using atomic variables with implicit compare-and-set low-level support.

    Seeing the complexity introduced by much more performant lock-free atomic integer you might want to restore to classic synchronization:

    public synchronized boolean saveIfHighest(int bid) {
        if (highestBid < bid) {
            highestBid = bid;
            return true;
        } else {
            return false;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working on a project to create simple file uploader site that
I am working on an NLP project that can create entity sets and compute
Currently am working on a project that requires me to create some XML for
I'm working on a project where I need to create a link that when
I'm working on a project that requires me to programmatically create MySQL users from
I'm working on a new project and for some reason decided to create two
The project I'm working on creates a local copy of the SQL Server database
I'm trying to create a simple flex4 project which involves some timers that trigger
I have been working on a project where user can create simple templates and
I'm working on a simple project to create tables and insert data into them,

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.