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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:57:36+00:00 2026-06-18T09:57:36+00:00

I need to insert into database which has two columns- ID Primary Key String

  • 0

I need to insert into database which has two columns-

ID Primary Key String
Data String

So that means ID should be unique every time , otherwise it will throw a duplicate row in unique index exception while inserting. And I need to choose ID in between this range 1-100000

So that means each thread should be using unique id always-

Below is the multithreaded program I wrote that will insert into database with different unique id’s every time after getting from ArrayBlockingQueue.

So this program will be thread safe or not? Or is there any other better way to get the unique ID’s every time for each thread? Or the below program can cause duplicate row in unique index ?

private static LinkedList<Integer> availableExistingIds = new LinkedList<Integer>();

public static void main(String[] args) {

        for (int i = 1; i <= 100000; i++) {
            availableExistingIds.add(i);
        }

        BlockingQueue<Integer> pool = new ArrayBlockingQueue<Integer>(200000, false, availableExistingIds);

        ExecutorService service = Executors.newFixedThreadPool(10);

            for (int i = 0; i < noOfTasks * noOfThreads; i++) {
                service.submit(new ThreadTask(pool));
            }
}   

class ThreadTask implements Runnable {

    private BlockingQueue<Integer> pool;
    private int id;

    public ThreadTask(BlockingQueue<Integer> pool) {
        this.pool = pool;
    }

    @Override
    public void run() {

         try {
            dbConnection = getDBConnection();
            preparedStatement = dbConnection.prepareStatement(INSERT_SQL);

            id = pool.take();

            preparedStatement.setString(1, String.valueOf(id));
            preparedStatement.setString(2, ACCOUNT);

            preparedStatement.executeUpdate();

       } finally {
         pool.offer(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-18T09:57:38+00:00Added an answer on June 18, 2026 at 9:57 am

    The pool.offer(id) means you put a used id back in the queue – so it can be reused later on by another thread. That is probably going to be an issue (since the queue is FIFO you will get a duplicate ID on the 100,001st insertion).

    In any case, it seems very complicated when a static AtomicInteger would do the same thing without having to use a queue:

    class ThreadTask implements Runnable {
    
        private final AtomicInteger id;
    
        ThreadTask(AtomicInteger id) {
            this.id = id; //in your main thread: id = new AtomicInteger(minId);
        }
    
        @Override
        public void run() {
            dbConnection = getDBConnection();
            preparedStatement = dbConnection.prepareStatement(INSERT_SQL);
    
            preparedStatement.setString(1, String.valueOf(id.getAndIncrement()));
            preparedStatement.setString(2, ACCOUNT);
    
            preparedStatement.executeUpdate();
        }
    }
    

    Note: as commented, your database can probably assign unique IDs for you.

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

Sidebar

Related Questions

I need to insert into database which has two columns- ID PrimaryKey String ACCOUNT
I need to insert data into a column in the database. Which hook should
i have asp.net project which has dropdownlist. i need to insert data into my
I need to copy data into an MSSQLServer 2005 database table which has an
I have a database that has two tables, one of which contains a foreign
Here my code, I need to insert into mysql database I have mysql,php,android 1)
I need to insert rows rows into a database, then in another thread read
I need a way to insert new articles straight into my MediaWiki database without
I have around 4000 entities that I need to insert into a Java App
I have a xml file which contains data I want to insert into 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.