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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:05:55+00:00 2026-06-10T17:05:55+00:00

I wrote a multithreaded program in which I am seeing some weird behavior. Below

  • 0

I wrote a multithreaded program in which I am seeing some weird behavior. Below is the code.

class Task implements Runnable {
      private Command command;
      private BlockingQueue<Integer> existPool;
      private BlockingQueue<Integer> newPool;
      private int existId;
      private int newId;


      public Task(Command command, BlockingQueue<Integer> pool1, BlockingQueue<Integer> pool2) {
            this.command = command;
            this.existPool = pool1;
            this.newPool = pool2;
      }

public void run() {
    if(command.getDataCriteria().equals(PDSLnPConstants.DATA_CRITERIA_PREVIOUS)) {
    try {
        // Getting existing id from the pool
        existId = existPool.take();
        attributeGetSetMethod(existId);
    } catch (Exception e) {
        getLogger().log(LogLevel.ERROR, e.getLocalizedMessage());
    } finally {
        // And releasing that existing ID for re-use
        existPool.offer(existId);       
    }
    }

//Something wrong happening here
else if(command.getDataCriteria().equals(PDSLnPConstants.DATA_CRITERIA_NEW)) {
    try {
        // Getting new id from the pool
        newId = newPool.take();
        attributeGetSetMethod(newId);
    } catch (Exception e) {
        getLogger().log(LogLevel.ERROR, e.getLocalizedMessage());
    } finally {
        // And releasing that new ID for re-use
        newPool.offer(newId);   
    }
    }
}
}

From the below code. above run method is getting called.

    for (int i = startRange; i <= endRange; i++) {
    availableExistingIds.add(i);
    }

    for (int n = newStartRange; n <= newEndRange; n++) {
    availableNewIds.add(n);
    }

    BlockingQueue<Integer> existIdPool = new ArrayBlockingQueue<Integer>(11, false, availableExistingIds);
    BlockingQueue<Integer> newIdPool = new ArrayBlockingQueue<Integer>(21, false, availableNewIds);


    GUID_ID_MAPPING = new LinkedHashMap<Integer, LinkedHashMap<Integer, String>>();
    long startTime = System.currentTimeMillis();
    long endTime = startTime + (durationOfRun*60*1000L);

    // Running for particular duration of time
    while(System.currentTimeMillis() <= endTime) {
    Command nextCommand = getNextCommandToExecute();
    Task nextCommandExecutorRunnable = new Task(nextCommand, existIdPool, newIdPool);
    executorService.submit(nextCommandExecutorRunnable); // Submit it for execution
    }

Problem Statement:-

One weird thing that I have just noticed is- In the below else if loop if you see my above code in run method if the command.getDataCriteria() is Previous then also it gets entered in the else if block(which is for New) which shouldn’t be happening right as I am doing a .equals check? Why this is happening?

else if(command.getDataCriteria().equals(PDSLnPConstants.DATA_CRITERIA_NEW)) {

Update:-

Below is the image from my console.enter image description here

in which you can clearly see, dataCriteria was New and it went to PREVIOUS block

  • 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-10T17:05:56+00:00Added an answer on June 10, 2026 at 5:05 pm

    If your code is really

    if(command.getDataCriteria().equals(PDSLnPConstants.DATA_CRITERIA_PREVIOUS)) {
       ...
    else if(command.getDataCriteria().equals(PDSLnPConstants.DATA_CRITERIA_NEW)) {
       ...
    }
    

    Then it is not possible that both ...s can get executed. It doesn’t depend on synchronization, threads, a bad equals implementation, or anything else. There’s something else you don’t understand (is it really the same run() call that is in both?), or possibly something I don’t understand about your problem statement (which is pretty confusing).

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

Sidebar

Related Questions

I wrote a Multithreaded program in Java given below :- public class Client {
I wrote a java class in order to perform multithreaded tasks, each task running
I wrote a multi-threaded program which does some CPU heavy computation with a lot
I wrote a C program which reads a dataset from a file and then
So I wrote a little multithreaded SMTP program. The problem is every time I
I wrote a class in charge of keeping track of error messages, which uses
I am trying to integrate some new code I wrote for programmatically interacting with
I wrote a symfony task to fill a database of sample data. Here's a
I wrote some script in a site. The script makes a new spreadsheet and
I am trying to write a simple multithreaded program in C#. It has a

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.