Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following program will sometimes throw exceptions when using StringBuilder, but will never throw an exception when using StringBuffer.</p> <p>Program:</p> <pre><code>public class StringBuilderConcurrent { static final StringBuilder sb = new StringBuilder(); // shared memory public static void main(String[] args) throws Exception { int NUM_WRITERS = 300; ArrayList&lt;WriterThread&gt; threads = new ArrayList&lt;WriterThread&gt;(NUM_WRITERS); for (int i = 0; i &lt; NUM_WRITERS; i++) { WriterThread wt = new WriterThread("writerThread" + i); threads.add(wt); wt.start(); } for (int i = 0; i &lt; threads.size(); i++) { threads.get(i).join(); } System.out.println(sb); } public static class WriterThread extends Thread { public WriterThread(String name) { super(name); } public void run() { String nameNl = this.getName() + "\n"; for (int i = 1; i &lt; 20; i++) { sb.append(nameNl); } } }; } </code></pre> <p>Because StringBuilder (<code>sb</code>) is not thread safe, having multiple threads write data to <code>sb</code> may result in <code>sb</code> becoming corrupted (e.g. unexpected null characters, a word's letters interspersed with some other word's letters). It's also possible for <code>sb</code>'s internal state to become inconsistent enough that an exception might be thrown:</p> <pre><code>Exception in thread "writerThread0" java.lang.ArrayIndexOutOfBoundsException at java.lang.System.arraycopy(Native Method) at java.lang.String.getChars(String.java:854) at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:391) at java.lang.StringBuilder.append(StringBuilder.java:119) at test.StringBuilderConcurrent$WriterThread.run(StringBuilderConcurrent.java:35) </code></pre> <p>The following program is identical to the first except it uses StringBuffer instead of StringBuilder. It will never encounter an ArrayIndexOutOfBoundsException.</p> <pre><code>public class StringBufferConcurrent { static final StringBuffer sb = new StringBuffer(); // shared memory public static void main(String[] args) throws Exception { int NUM_WRITERS = 300; ArrayList&lt;WriterThread&gt; threads = new ArrayList&lt;WriterThread&gt;(NUM_WRITERS); for (int i = 0; i &lt; NUM_WRITERS; i++) { WriterThread wt = new WriterThread("writerThread" + i); threads.add(wt); wt.start(); } for (int i = 0; i &lt; threads.size(); i++) { threads.get(i).join(); } System.out.println(sb); } public static class WriterThread extends Thread { public WriterThread(String name) { super(name); } public void run() { String nameNl = this.getName() + "\n"; for (int i = 1; i &lt; 20; i++) { sb.append(nameNl); } } }; } </code></pre> <p>Whether these programs are representative of a "real world" problem is a fairly subjective question. I'll leave that judgement up to the audience. </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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