开发者

Why does a single thread make my Java Program so much faster?

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 Vec开发者_运维问答torizeView(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!


I think that what you are doing is appearing to make the database load faster because the VectorizeView method is returning before the data has been loaded. The load is then proceeding in the background, and completing in (probably) the same time as before.

You could test this theory by adding a thread.join() call after the thread.start() call.


If this is what is going on, you probably need to do something to stop other parts of your application from accessing the table object before loading has completed. Otherwise your application is liable to behave incorrectly if the user does something too soon after launch.


FWIW, loading 100 or 500 records from a database should be quick, unless the query itself is expensive for the database. That shouldn't be the case for a simple select from a table ... unless you are actually selecting from a view rather than the table, and the view is poorly designed. Either way, you probably would be better off focussing on why such a simple query is taking so long, rather than trying to run it in a separate thread.


In your follow-up you say that the version with the join after the start is just as fast as the version without it.

My first reaction is to say: "Leave the join there. You've fixed the problem."

But this doesn't explain what is actually going on. And I'm now completely baffled. The best I can think of is that something your application is doing before this on the current thread is the cause of this.

Maybe you should investigate what the application is doing in the period in which this is occurring. See if you can figure out where all the time is being spent.

  • Take a thread dump and look at the threads.
  • Run it under the debugger to see where the "pause" is occurring.
  • Profile it.
  • Set the application logging to a high level and see if there are any clues.
  • Check the database logs.
  • Etcetera


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).


Are you sure that it is faster? Since you start separate thread, you will return table immediately. But are you sure that you measure time after it's fully populated with data?


Update To measure time correctly, save runner object somewhere and call runner.join(). You can even to it in the same method for testing.


Ok, I think that if you examine table at the end of this method you will find it's empty. That's because start starts running the thread in the background, and you immediately return table without the background thread having a chance to populate it. So it appears to be going faster but actually isn't.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜