Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>OK, so this looks like a classic case of over optimization. Object creation isn't that expensive that you need to rewrite the same string buffer, especially if this is called every 300-400ms.</p> <p>I'll try to address every possible scenario: <strong>Exponential growth</strong> The above code is assigned to a new thread every 300ms but the list is enormous and takes over 300ms to serialize. If this is the case you are basically choking your resources and it is only a matter of time before the application crashes. If this is the case, you should see the CPU constantly rising. The solution would be to:</p> <ol> <li>Limit the number of threads that can run concurrently so you don't kill the application</li> <li>Concurrently build the json object and merge the result so building a single json takes less then 300ms.</li> </ol> <p><strong>Speedups</strong> OK, so the list isn't clonable which I'm assuming means it isn't really a list but rather some sort of queue implemented as a list interface. So leaving synchronization as is I would do this:</p> <pre><code>public static final int JSON_LENGTH = 250; //you probably know this public static String toJson(final List&lt;QuotesData&gt; quotesData) { jsonVal = new StringBuilder(JSON_LENGTH * quotesData.size()); jsonVal.append("{"); synchronized (quotesData) { for (QuotesData quote : quotesData) { jsonVal.append("\"").append(quote.getSymbol()).append("\":[{") .append("\"ask\":\"").append(quote.getAsk()).append("\",") .append("\"bid\":\"").append(quote.getBid()).append("\",") .append("\"time\":\"").append(quote.getDateTime()).append("\"}],"); } // much much faster than replace jsonVal.setCharAt(jsonVal.length()-1, '}'); return jsonVal.toString(); } } </code></pre> <p>Most of the changes are cosmetic, and I'm pretty sure that the JIT would already optimize them. Only difference I would do is use StringBuilder and create a new one each time and don't use <em>.replace()</em>. But to stress my point further unless you fit the first description (exponential growth) I doubt the problem is here. I would look at you list implementation first.</p>
 

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