title: Inserting multiple Strings into a JTextPane with a TitledBorder in a single Thread locks cause programm to lock up
body: <p>I have encountered a <strong>very</strong> strange problem when trying to insert multiple Strings into a JTextPane that has a TitledBorder (this is important, the problem seems to only occur with TitledBorder specifically, other Borders or no Border at all work fine) from a Thread. The (for this problem) significant part of my test code looks like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>JTextPane myTextPane = new JTextPane();
  myTextPane.setBorder(new TitledBorder("Some title"));
  StyledDocument doc = myTextPane.getStyledDocument();
  SimpleAttributeSet sas = new SimpleAttributeSet();
  StyleConstants.setForeground(sas, Color.BLACK);
  
  private void insertTwoStrings()
  {
      new Thread(new Runnable()
      {
          @Override
          public void run()
          {
              docTest.insertString(docTest.getLength(), "first string ", sas);
              docTest.insertString(docTest.getLength(), "second string\n", sas);
          }
      }).start();
  }
  </code></pre>
  
  <p>Now to the problem: the insertTwoStrings() method works fine <strong>sometimes</strong>, but other times it fails <strong>very badly</strong> in a way that locks up the entire application (i have to kill the process to shut the application down). So i opened the program in the debugger and replicated the problem there, when the problematic Thread got locked up i suspended it and took a closer look at the program counter position:</p>
  
  <p><img src="https://i.stack.imgur.com/qNxAm.png" alt="Current position of program counter"></p>
  
  <p>It seems like this synchronized(this) is the cause of my problem. Is this actually a bug or am i making some kind of mistake?</p>
  
  <p>In case someone wants to replicate this, for the problem to occur you have to meet all of the following three criteria:</p>
  
  <ol>
  <li>inserting text into a JTextPane (i.e. into its StyledDocument) <strong>from a Thread</strong></li>
  <li>invoking StyledDocument.insertString(...) <strong>multiple times</strong> within the thread, not just once</li>
  <li>the JTextPane must have a TitledBorder</li>
  </ol>
  
  <p>Executing the Thread will still work <em>sometimes</em>, but every now and then it will fail and lock up the entire program.</p>