Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does a single thread make my Java Program so much faster?
    primarykey
    data
    text
    <p>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:</p> <pre><code>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 &lt;= 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; } </code></pre> <p>The only thing i really changed was adding the thread 'runner' and the performance increased exponentially. Pulling 500 records occurs almost instantly this way.</p> <p>The method looked like this before:</p> <pre><code>public Vector VectorizeTable(String tableName) { Vector&lt;Vector&gt; table = new Vector&lt;Vector&gt;(); 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 &lt;= 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; } </code></pre> <p>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.</p> <p>Any information anyone could give would be greatly appreciated. I'm a noob on threads XO</p> <p>If you need any other additional information to help understand what is going on let me know!</p> <p><strong>Answer:</strong></p> <p>Look at Aaron's answer this wasn't an issue with threads at all. I feel very noobish right now :(. THANKS @Aaron!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload