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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:36:26+00:00 2026-06-07T01:36:26+00:00

I have a simple program. The main idea is that I have a list

  • 0

I have a simple program. The main idea is that I have a list of names stored in MySQL database and I want to do some operations on these names concurrently, but of course, each thread should work in a separate name. The next thread should work on the next name that taken by the previous thread. I created thread pool, I create new threads inside the loop and then execute the runnable so the operations on that names performed. In this example, the operation is printing the name that is selected from DB. The program is skipping some names from the database, and repeated the last name 6 times. What is wrong in my program? I’m still new to threads, please excuse my mistakes.

This is the main function:

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {

    public static volatile ResultSet resultSet = null;
    private static Statement statement = null;

    public static void main(String[] args) throws SQLException 

    {
        DBConnection.ConnectDB(); //connect to database


          statement = DBConnection.con.createStatement();
          resultSet = statement.executeQuery("select Name from schema.table1"); //select statement

          String name = null;


           // create ExecutorService to manage threads 
          ExecutorService threadExecutor = Executors.newFixedThreadPool(3 );

          // create and name each runnable  
          while(resultSet.next()) 
             {
                 name=resultSet.getString("Name");
                 MyRunnable task1 = new  MyRunnable( name);
                 threadExecutor.execute( task1 );
          }


        // This will make the executor accept no new threads
        // and finish all existing threads in the queue

             threadExecutor.shutdown();
        // Wait until all threads are finish
        while (! threadExecutor.isTerminated()) {

        }
        System.out.println("Finished all threads");
    }
}

And the MyRunnable class:

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;

public class MyRunnable implements Runnable{
private static String nn;


MyRunnable (String ss)  { synchronized (this) { 
    this.nn=ss;
}
}


public void run() 
{

    System.out.println("hello "+ nn);
}
}
  • 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-07T01:36:28+00:00Added an answer on June 7, 2026 at 1:36 am

    This is certainly one issue. Remove the static.

    private static String nn;
    

    becomes

    private String nn;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple program. I want to set the path and some properties
I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return
Say I have simple program that emulates a board game with a number of
I have a simple program that checks webpages for strings, example: Private Sub Button1_Click(ByVal
I have this simple program that computes salaries for four different worker types. It's
I have a simple program that creates a thread, loops twenty times and then
I have an idea for a program that will do a particular job for
I'm planning to make a very simple program using php and mySQL. The main
I have made a program that reads voltage and current values of some diode
I have a simple homework... to make a C program that takes 3 values

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.