Which Swing component methods are thread safe?

前端 未结 5 868
情书的邮戳
情书的邮戳 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: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

提交回复
热议问题