JTextArea safe thread?

I have code that does some initialization (including creating an object JTextArea), starts three separate threads, and then these threads try to update JTextArea(i.e. append()to it), but it doesn't work at all. Nothing is displayed on JTextArea(however, during initialization, I print several test lines on it, and this works fine). What's happening? How can i fix this? In addition, each of these threads resets a random amount of time each time it needs to update JTextArea.

Sorry, I did not provide any code, it was all distributed across several files.

+2
source share
5 answers

Although I believe the API stated that JTextArea # append (...) is thread safe, I have heard of problems with it and recommend that this be called only on EDT. A classic example of this is using SwingWorker and adding to JTextArea in a process method, causing the publication.

It will be difficult for me to make any specific suggestions, but without code. I really wonder, but if you put the EDT in a place somewhere in your code.

Edit: according to your comment check out this guide: Concurrency in Swing


2: , Java , JTextArea:

doc.insertString(doc.getLength(), str, null);

:

  • int len=doc.getLength();
  • doc.insertString(len,str,null);

, , , , 1 2, .

+4

Java 1.6 JTextArea.append :

. , null null .

, Swing . . " " . .

JDK7 :

. , null null .

Document ( JTextArea , ), , . . AWT EDT, - Swing.

+3

JTextArea.append(..) , .

javadoc .append() :

Does nothing if the model is null or the string is null or empty.

, , JTextArea ( ).

+2

, Document. , , JTextArea . , , append, . , Debian Oracle jre 6 ( Win7 java 6 64 ) .

, :

  • getLength() insertString(), BadLocationException s. . :)
  • BadLocationException. , , .

, getLength() insertString(), , JTextArea (. Tom Hawtin ). , , insertString , , . 10000, 100000. jdk 7 JTextArea.append, , , JTextArea .

0 - , .

. , . setCaretPosition EDT. . . .

, , , . , Document.insertString , getLength .

PlainDocument append, getLength insertString . , . .

BTW: . , , .

:

import java.awt.*;
import java.util.concurrent.CountDownLatch;
import javax.swing.*;
import javax.swing.text.*;

class SafePlainDocument extends PlainDocument
{
  public void append(String s)
  {
    writeLock();
    try {
      insertString(getLength(), s,  null);
    }
    catch (BadLocationException e) {
      e.printStackTrace();
    }
    finally
    {
      writeUnlock();
    }
  }
}

public class StressJText
{
  public static CountDownLatch m_latch;
  public static SafePlainDocument m_doc;
  public static JTextArea m_ta;

  static class MyThread extends Thread
  {
    SafePlainDocument m_doc;
    JTextArea m_ta;

    public MyThread(SafePlainDocument doc)
    {
      m_doc = doc;
    }

    public void run() 
    {
      for (int i=1; i<=100000; i++) {
        String s = String.format("%19s %9d\n", getName(), i);
        m_doc.append(s);
      }
      StressJText.m_latch.countDown();
    }
  }

  public static void main(String sArgs[])
  {
    System.out.println("hello");
    final int cThreads = 5;
    m_latch = new CountDownLatch(cThreads);
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
          JFrame frame = new JFrame();
          m_ta = new JTextArea();
          m_doc = new SafePlainDocument();
          m_ta.setDocument(m_doc);
          m_ta.setColumns(50);
          m_ta.setRows(20);
          JScrollPane scrollPane = new javax.swing.JScrollPane();
          scrollPane.setViewportView(m_ta);
          frame.add(scrollPane);
          frame.pack();
          frame.setVisible(true);

          for (int it=1; it<=cThreads; it++) {
            MyThread t = new MyThread(m_doc);
            t.start();
          }
        }
    });
    try {
      m_latch.await();
    }
    catch (InterruptedException ie) {
      ie.printStackTrace();
    }
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
          System.out.println("tf len: " + m_ta.getText().length());
          System.out.println("doc len: " + m_doc.getLength());
          System.exit(0);
        }
    });
  }
}
+2

JTextArea ?

. . , append . Java 7 documentaion of AbstractDocument.insertString , .

AbstractDocument.insertString . . gui .

JTextArea.append? , Document. PlainDocument DefaultStyledDocument . . - , , append EDT.

: append : : getLength insertString, 2 . insertString(getLength(), ...). . AbstractDocument.writeLock , .

+2

All Articles