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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:46:10+00:00 2026-06-10T09:46:10+00:00

Below is the code in which in the run method , I am always

  • 0

Below is the code in which in the run method, I am always trying to get unique id from the availableExistingIds and releasing it at the same moment by making a linked list order, but in certain cases I found out that, I am getting NoSuchElementException and id is zero few times which I think should not be the case anytime.

class IdPool {
    private final LinkedList<Integer> availableExistingIds = new LinkedList<Integer>();

    public IdPool() {
        for (int i = 1; i <= 1000; i++) {
            availableExistingIds.add(i);
        }
    }

    public synchronized Integer getExistingId() {
        return availableExistingIds.removeFirst();
    }

    public synchronized void releaseExistingId(Integer id) {
        availableExistingIds.add(id);
    }
}


class ThreadNewTask implements Runnable {
    private IdPool idPool;
    private int id;

    public ThreadNewTask(IdPool idPool) {
        this.idPool = idPool;
    }

    public void run() {
        try {
            id = idPool.getExistingId();
            //Anything wrong here?  
                    if(id==0) {
                        System.out.println("Found Zero");
                    }
            someMethod(id);
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            idPool.releaseExistingId(id);
        }
    }

    // This method needs to be synchronized or not?
            private synchronized void someMethod(Integer id) {
                System.out.println("Task: " +id);
                // and do other calcuations whatever you need to do in your program
            }
}

Problem Statement:-

How can I avoid this zero id case here in my code? One scenario under which I can get id = 0 is when the id pool is exhausted (empty). When that happens, the line:

id = idPool.getExistingId();

will fail with a NoSuchElementException. In this case, the finally block will run:

idPool.releaseExistingId(id);

But id will still have its default value of 0 since the first line failed. So I end up “releasing” 0 and adding it back to the id pool even though it was never in the pool to start with. Then a later task could take 0 legitimately. And that’s what I don’t need. Can anyone suggest me how to overcome this scenario in my code? I always want id should be in the range of 1 to 1000.

  • 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-10T09:46:12+00:00Added an answer on June 10, 2026 at 9:46 am

    why don’t you modify your code so that instead of crashing when there are no available ids, it waits for one to become available?

    Otherwise, every time you have too much threads working at once, the pool is going to be exhausted, and you are going to have to deal with a lot of failing threads. Also the synchronization work is taken care of for you automatically.

    EDIT: here is the modified code

    class ThreadNewTask implements Runnable {
      private BlockingQueue<Integer> pool;
      private int id;
    
      public ThreadNewTask(BlockingQueue<Integer> pool) {
        this.pool = pool;
      }
    
      public void run() {
        try {
            id = pool.take();
            someMethod(id);
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            pool.offer(id);
        }
      }
    
      private void someMethod(Integer id) {
        System.out.println("Task: " +id);
                // and do other calcuations whatever you need to do in your program
      }
    }  
    

    And then you initialize the pool with something like this:

    LinkedList<Integer> availableExistingIds = new LinkedList<Integer>();
    for (int i = 1; i <= 1000; i++) {
      availableExistingIds.add(i);
    }
    BlockingQueue<Integer> pool = new ArrayBlockingQueue<Integer>(1000, false, availableExistingIds);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the code below, I am trying to execute a method, which returns a
I am using the below method between the 50lines of JQuery Code. From the
Given the code below, ReadProcessMemory always returns an array of zeros. I'm trying to
I have the php code below which help me get a photo's thumbnail image
Currently i'm using below code which works well. $(#topperAtBaseLevel:visible, #lowerAtBaseLevel:visible, #midAtBaseLevel).hide(); any optimised code?
I have below code which overrides equals() and hashcode() methods. public boolean equals(Object obj)
Can anyone help me to rewrite the below code which use a fade effect
I want to know is below code correct ? I have following code which
Which code snippet will give better performance? The below code segments were written in
Below is the code which I wrote to display a tooltip content. As the

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.