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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:03:24+00:00 2026-05-23T17:03:24+00:00

I have been given the task of creating a sql database and creating a

  • 0

I have been given the task of creating a sql database and creating a GUI in Java to access it with. I pretty much have it but I have a question about threads. Before today I did not use any threads in my program and as a result just to pull 150 records from the database i had to wait around 5 – 10 seconds. This was very inconvenient and I was not sure if i could fix the issue. Today I looked on the internet about using threads in programs similar to mine and i decided to just use one thread in this method:

public Vector VectorizeView(final String viewName) {
    final Vector table = new Vector();
    int cCount = 0;
    try {
        cCount = getColumnCount(viewName);
    } catch (SQLException e1) {
        e1.printStackTrace();
    }
    final int viewNameCount = cCount;

    Thread runner = new Thread(){

        public void run(){
            try {
                Connection connection = DriverManager.getConnection(getUrl(),
                        getUser(), getPassword());
                Statement statement = connection.createStatement();
                ResultSet result = statement.executeQuery("Select * FROM "
                        + viewName);
                while (result.next()) {
                    Vector row = new Vector();
                    for (int i = 1; i <= viewNameCount; i++) {
                        String resultString = result.getString(i);
                        if (result.wasNull()) {
                            resultString = "NULL";
                        } else {
                            resultString = result.getString(i);
                        }
                        row.addElement(resultString);

                    }
                    table.addElement(row);
                }
            } catch (SQLException e) {

                e.printStackTrace();
            }
        }
    };
    runner.start();
    return table;

}

The only thing i really changed was adding the thread ‘runner’ and the performance increased exponentially. Pulling 500 records occurs almost instantly this way.

The method looked like this before:

public Vector VectorizeTable(String tableName) {

    Vector<Vector> table = new Vector<Vector>();
    try {
        Connection connection = DriverManager.getConnection(getUrl(),
                getUser(), getPassword());
        Statement statement = connection.createStatement();
        ResultSet result = statement.executeQuery("Select * FROM "
                + tableName);
        while (result.next()) {
            Vector row = new Vector();
            for (int i = 1; i <= this.getColumnCount(tableName); i++) {
                String resultString = result.getString(i);
                if (result.wasNull()) {
                    resultString = "NULL";
                } else {
                    resultString = result.getString(i);
                }
                row.addElement(resultString);

            }
            table.addElement(row);
        }
    } catch (SQLException e) {

        e.printStackTrace();
    }
    return table;
}

My question is why is the method with the thread so much faster than the one without? I don’t use multiple threads anywhere in my program. I have looked online but nothing seems to answer my question.

Any information anyone could give would be greatly appreciated. I’m a noob on threads XO

If you need any other additional information to help understand what is going on let me know!

Answer:

Look at Aaron’s answer this wasn’t an issue with threads at all. I feel very noobish right now :(. THANKS @Aaron!

  • 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-05-23T17:03:24+00:00Added an answer on May 23, 2026 at 5:03 pm

    It looks like you kick off (i.e. start) a background thread to perform the query, but you don’t join to wait for the computation to complete. When you return table, it won’t be filled in with the results of the query yet — the other thread will fill it in over time, after your method returns. The method returns almost instantly, because it’s doing no real work.

    If you want to ensure that the data is loaded before the method returns, you’ll need to call runner.join(). If you do so, you’ll see that loading the data is taking just as long as it did before. The only difference with the new code is that the work is performed in a separate thread of execution, allowing the rest of your code to get on with other work that it needs to perform. Note that failing to call join could lead to errors if code in your main thread tries to use the data in the Vector before it’s actually filled in by the background thread.

    Update: I just noticed that you’re also precomputing getColumnCount in the multi-threaded version, while in the single-threaded version you’re computing it for each iteration of the inner loop. Depending on the complexity of that method, that might explain part of the speedup (if there is any).

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

Sidebar

Related Questions

I have been given a new task from the client which is basically creating
I have been given the task of using java to produce a Sin table,
I have been given a task of reproducing the issue/testing the unauthorized access to
I have been given the enviable task of migrating a legacy SQL Server 2000
I have been given the task of porting an Access application to Eclipse RCP/RAP.
I have recently been given a task to add the ability to interact with
I work in the Systems & admin team and have been given the task
I have been given the task to fix an Embedded Operating System which is
I have been given to the task of writing a coding standards document and
Hi I've been given the task of creating an N-Teir website using the Entity

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.