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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:11:50+00:00 2026-06-04T11:11:50+00:00

How do I make sure each thread is using different unique ID and that

  • 0

How do I make sure each thread is using different unique ID and that ID should be in between startExistingRange and endExistingRange. As I am worried because the program is supposed to run for 60 minutes and before the 60 minutes it is possible that all the id’s would have been used then what should I do. Should I reset the variables? What can be the best practice?

For Example:- Thread 1 will be using 25, Thread 2 will use 45 etc etc.

class ThreadTask implements Runnable {
    private int id;

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

    public void run() {
        System.out.println("Thread " + id);
    }
}

public class TestPool {

    public static void main(String[] args) {
        int size = 10;
        int durationOfRun = 60;
        int startExistingRange = 1;
        int endExistingRange = 1000;

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

        // queue some tasks
        long startTime = System.currentTimeMillis();
        long endTime = startTime + (durationOfRun*60*1000);

        // Running it for 60 minutes
        while(System.currentTimeMillis() <= endTime) {
/* I want each thread uses different unique ID between startExistingRange
 and endExistingRange */
            service.submit(new ThreadTask(What should I pass 
                 here so that each thread is using different ID));
        }

        // wait for termination        
        service.shutdown();
        service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); 
    }
}

Updated:-

public class TestingPool {

    public static void main(String[] args) throws InterruptedException {
        int size = 10;
        int durationOfRun = 1;
        IdPool idPool = new IdPool();   
        // create thread pool with given size
        ExecutorService service = Executors.newFixedThreadPool(size); 

        // queue some tasks
        long startTime = System.currentTimeMillis();
        long endTime = startTime + (durationOfRun * 60 * 1000L);

        // Getting and releasing id in while loop
        while(System.currentTimeMillis() <= endTime) {
            Integer id = idPool.getId();
            service.submit(new ThreadTask(idPool, id));
            idPool.releaseId(id);
        }

        // wait for termination        
        service.shutdown();
        service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); 
    }

}

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

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

    public synchronized Integer getId() {
        return availableIds.removeFirst();
    }

    public synchronized void releaseId(Integer id) {
        availableIds.add(id);
    }
}


class ThreadTask implements Runnable {
    private IdPool idPool;
    private int kk;

    public ThreadTask(IdPool idPool, int s) {
        this.idPool = idPool;
        this.kk = s;
    }

    public void run() {
        //Integer id = idPool.getId();
        System.out.println("Task " + kk);
        //idPool.releaseId(id);
    }
}
  • 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-04T11:11:52+00:00Added an answer on June 4, 2026 at 11:11 am

    Instead of passing an ID to your task at creation time, have your task get their ID from a pool of available IDs. Since you have 10 threads, you only need 10 IDs. Each task takes an ID from the pool at startup, and releases it to the pool when done. Of course, the pool needs to be thread safe:

    public class IdPool {
        private final LinkedList<Integer> availableIds = new LinkedList<Integer>();
    
        public IdPool() {
            for (int i = 1; i <= 1000; i++) {
                availableIds.add(i);
            }
        }
    
        public synchronized Integer getId() {
            return availabeIds.removeFirst();
        }
    
        public synchronized void releaseId(Integer id) {
            availableIds.add(id);
        }
    }
    
    
    class ThreadTask implements Runnable {
        private IdPool idPool;
    
        public ThreadTask(IdPool idPool) {
            this.idPool = idPool;
        }
    
        public void run() {
            Integer id = idPool.getId();
            System.out.println("Task " + id);
            idPool.releaseId(id);
        }
    }
    
    public class Main {
        public static void main(String[] args) throws InterruptedException {
            int size = 10;
            int durationOfRun = 60;
            IdPool idPool = new IdPool();   
            // create thread pool with given size
            ExecutorService service = Executors.newFixedThreadPool(size); 
    
            // queue some tasks
            long startTime = System.currentTimeMillis();
            long endTime = startTime + (durationOfRun * 60 * 1000L);
    
            // Running it for 60 minutes
            while(System.currentTimeMillis() <= endTime) {
                service.submit(new ThreadTask(idPool));
            }
    
            // wait for termination        
            service.shutdown();
            service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just to make sure I'm understanding shallow copies of reference types correctly and that
How can I make sure in the following code snippet that IDataReader is disposed
I'm writing a multithreaded decompressor in python. Each thread needs to access a different
The program is to create several threads where each thread increments a shared variable
I'm trying to make sure that I don't leave any loose ends open in
If the locks make sure only one thread accesses the locked data at a
I am working on a multi-threading program in Ruby, just want to make sure
Edit Make sure you don't juxtapose the request and response objects. things will be
I want to make sure a string has only characters in this range [a-z]
I want to make sure I'm not inserting a duplicate row into my table

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.