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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:04:27+00:00 2026-06-18T16:04:27+00:00

I am working on a project in which I need to make sure each

  • 0

I am working on a project in which I need to make sure each thread get’s unique ID every time in a particular range. For example-

If number of threads is 2 and number of tasks is 10 then each thread will be performing 10 tasks. So that means 2 thread will be doing 20 tasks.

So what I am looking for something like this-

In the above example, first thread should be using id between 1 and 10 and second thread should be using id between 11 and 20.

Another example. So suppose if I have 3 thread and number of tasks as 20 then first thread should be using ID between 1 and 20 and second thread should be between 21 and 40 and third thread in between 41 and 60.

Below is the code I have so far, what it does is that it will get id as an AtomicInteger and I am getting unique id every time starting from 1.

class ThreadTask implements Runnable {
    private final AtomicInteger id;
    private int noOfTasks;

    public ThreadTask(AtomicInteger id2, int noOfTasks) {
        this.id = id2;
        this.noOfTasks = noOfTasks;
    }

    @Override
    public void run() {

        while(noOfTasks > 0) {
            System.out.println(id.getAndIncrement());
              // And use this id for other things.
        noOfTasks--;
         }
    }
}

public class XMPTest {

    public static void main(String[] args) {

        final int noOfThreads = 2;
        final int noOfTasks = 10;

        final AtomicInteger id = new AtomicInteger(1);

        //create thread pool with given size 
        ExecutorService service = Executors.newFixedThreadPool(noOfThreads);

        // queue some tasks 
        for (int i = 0; i < noOfThreads; i++) {
            service.submit(new ThreadTask(id, noOfTasks));
        }
    }
}

How can I restrict this such that thread 1 should get id between 1 and 10 and second thread should get id between 11 and 20

Updated Code:-
I am trying the below code

public static void main(String[] args) {

    final int noOfThreads = 2;
    final int noOfTasks = 10;

    //create thread pool with given size 
    ExecutorService service = Executors.newFixedThreadPool(noOfThreads);

    // queue some tasks 
    for (int i = 0, int nextId = 1; i < noOfThreads; i++, nextId += noOfTasks) {
        service.submit(new ThreadTask(nextId, noOfTasks));
    }
}


class ThreadTask implements Runnable {
    private final int id;
    private int noOfTasks;

    public ThreadTask(int nextId, int noOfTasks) {
        this.id = nextId;
        this.noOfTasks = noOfTasks;
    }

    public void run() {

        for (int i = id; i <= noOfTasks; i++) {
            System.out.println(i);
        }
    }
}

So for the first thread it is printing out 1 – 10 which is fine. But in the second thread, nextId is 11 and noOfTasks is 10 so my for loop going to work in the run method for second time.

  • 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-18T16:04:29+00:00Added an answer on June 18, 2026 at 4:04 pm

    Split up the range however you want in the singlethreaded code before you launch your threads, then pass the range to each thread.

    For example:

    public static void main(String[] args) {
    
        final int noOfThreads = 2;
        final int noOfTasks = 10;
    
        //create thread pool with given size 
        ExecutorService service = Executors.newFixedThreadPool(noOfThreads);
    
        // queue some tasks 
        for (int i = 0, int nextId = 1; i < noOfThreads; i++, nextId += noOfTasks) {
            service.submit(new ThreadTask(nextId, noOfTasks));
        }
    }
    
    class ThreadTask implements Runnable {
        private final int id;
        private int noOfTasks;
    
        public ThreadTask(int nextId, int noOfTasks) {
            this.id = nextId;
            this.noOfTasks = noOfTasks;
        }
    
        public void run() {
    
            for (int i = id; i < id + noOfTasks; i++) {
                System.out.println(i);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a project in which I need to change the time
I am working on a project in which I need to download and parse
I am working on a project in which I need to generate 8 random
I'm working on a project in which I need to process a huge amount
Currently we are working on a project in which we need to come up
Currently I am working with the project in which I need to parse complex
I am working on a homework project in which we need to generate a
I'm working on using a project which uses jQuery, and I need to dynamically
I need to make a simple asp.net MVC project, which contains a GridView. I
I am working on a project in which I need to know the current

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.