Which Swing component methods are thread safe?

前端 未结 5 838
情书的邮戳
情书的邮戳 2020-11-27 05:23

According to Swing tutorial:

Some Swing component methods are labelled \"thread safe\" in the API specification; these can be safely invoked from any

相关标签:
5条回答
  • 2020-11-27 05:28

    But you already have the answer: only those methods which are specifically documented as being thread-safe in the method JavaDoc, are threadsafe! this is from JTextComponent.setText

     * This method is thread safe, although most Swing methods
     * are not. Please see 
     * <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How
     * to Use Threads</A> for more information.     
    

    If the method documentation doesn't say it's safe, then it isn't safe: access to the JavaDoc is therefore critical when coding against Swing

    0 讨论(0)
  • 2020-11-27 05:29

    In Java 7, previously thread safe methods of the view components rooted in JTextComponent are no longer thread safe. A typical workaround using EventQueue.invokeLater() is shown here. The remaining model-related methods, listed here, remain thread safe.


    • JTextComponent
      • replaceSelection()
      • setText()
      • print()
      • getPrintable()

    • JTextPane
      • replaceSelection()
      • insertComponent()
      • insertIcon()
      • setLogicalStyle()
      • setCharacterAttributes()
      • setParagraphAttributes()

    • JTextArea
      • insert()
      • append()
      • replaceRange()
    0 讨论(0)
  • 2020-11-27 05:34

    Google taught me that at least those are threadsafe. Here's an overview for the case that the link get broken again:


    • JTextPane
      • replaceSelection()
      • insertComponent()
      • insertIcon()
      • setLogicalStyle()
      • setCharacterAttributes()
      • setParagraphAttributes()

    • JTextArea
      • insert()
      • append()
      • replaceRange()

    • JTextComponent
      • replaceSelection()
      • setText()
      • print()
      • getPrintable()

    • UndoManager
      • All methods.

    • DefaultStyledDocument
      • insert()
      • setLogicalStyle()
      • setCharacterAttributes()
      • setParagraphAttributes()

    • StyleContext
      • addAttribute()
      • addAttributes()
      • removeAttribute()
      • removeAttributes()
      • reclaim()

    • AbstractDocument
      • render()
      • remove()
      • insertString()
      • createPosition()

    • PlainDocument
      • insertString()

    • HTMLDocument
      • setParagraphAttributes()
    0 讨论(0)
  • 2020-11-27 05:42

    For a list of classes with the comment in the javadocs & src files "is thread safe" returns the following

    JEditorPane
    JTextArea
    AbstractDocument
    DefaultCaret
    DefaultStyledDocument
    JTextComponent    
    PlainDocument
    StyleContext    
    HTMLDocument
    UndoManager
    

    This is not saying that there are others documented or undocumented within the src that are thread safe.

    It strikes me as a rather strange question but I would treat most components as not being threadsafe and since Swing is a single threaded model and all updates need to happen on the event dispatcher thread this is pretty easy to do.

    0 讨论(0)
  • 2020-11-27 05:54

    But what are these Swing component methods that are labelled "thread safe"?

    Most Swing components' methods are NOT thread safe. But some are. To find out which ones, you have no option but to peruse the javadocs for your target components. A carefully constructed google search might quicken the process.

    Are there actually any?

    Yes there are indeed. Generally speaking, if you are working with Swing components, it is likely that you are going to have to invoke both thread-safe and non-thread-safe methods. Since most methods are non-thread-safe, I prefer to err on the side of caution, and perform all actions on them in a thread-safe manner anyway.

    HTH


    Not exhaustive list.

    DefaultStyledDocument:

    • protected void insert(int offset, DefaultStyledDocument.ElementSpec[] data) throws BadLocationException
    • public void setLogicalStyle(int pos, Style s)
    • public void setCharacterAttributes(int offset, int length, AttributeSet s, boolean replace)
    • public void setParagraphAttributes(int offset, int length, AttributeSet s, boolean replace)

    javax.swing.text.AbstractDocument:

    • public void render(Runnable r)
    • public void remove(int offs, int len) throws BadLocationException
    • public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
    • public Position createPosition(int offs) throws BadLocationException

    javax.swing.undo.UndoManager:
    Class is threadsafe

    0 讨论(0)
提交回复
热议问题