根据Swing教程:
Some Swing component methods are labelled "thread safe" in the API specification; these can be safely invoked from any thread. All other Swing component methods must be invoked from the event dispatch thread. Programs that ignore this rule may function correctly most of the time, but are subject to unpredictable errors that are difficult to reproduce.
但是这些标记为"线程安全"的Swing组件方法是什么? 真的有吗?
更新/赏金:
是否有 complete list 安全的摆动方法? (线程安全的Swing方法似乎很少见,因此这样的列表不能太长...)
- 2021-1-121 #
- 2021-1-122 #
But what are these Swing component methods that are labelled "thread safe"?
大多数Swing组件的方法都不是线程安全的.但是有些.要找出哪些组件,别无选择,只能仔细阅读javadocs作为目标组件.精心构建的Google搜索可能会加快这一过程。
Are there actually any?
是的,确实有.一般来说,如果您正在使用Swing组件,则可能必须同时调用线程安全和非线程安全方法.由于大多数方法都是非线程安全的,因此我更倾向于谨慎行事,无论如何都应以线程安全的方式对其执行所有操作。
HTH
列表不完整。
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:
类是线程安全的 - 2021-1-123 #
有关在javadocs&src文件中带有注释的类的列表,"是线程安全的" 返回以下内容
JEditorPane JTextArea AbstractDocument DefaultCaret DefaultStyledDocument JTextComponent PlainDocument StyleContext HTMLDocument UndoManager
这并不是说src中还有其他文件记录或未记录的线程安全记录。
这让我感到很奇怪,但我会将大多数组件视为 not 具有线程安全性,并且由于Swing是单线程模型,并且所有更新都需要在事件分派器线程上进行,因此这非常容易做到.
- 2021-1-124 #
但是您已经有了答案:只有那些方法,它们是 specifically documented 因为在JavaDoc方法中是线程安全的,所以是线程安全的! 这是来自
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.
如果方法文档没有说它安全,那么那也不安全:因此,在针对Swing进行编码时,访问JavaDoc至关重要
- 2021-1-125 #
在Java 7中,以前的线程安全方法 查看以
JTextComponent
为根的组件 不再是线程安全的.使用EventQueue.invokeLater()
的典型解决方法 显示在这里.此处列出的其余与模型相关的方法仍然保持线程安全。
JTextComponent
replaceSelection()
setText()
print()
getPrintable()
JTextPane
replaceSelection()
insertComponent()
insertIcon()
setLogicalStyle()
setCharacterAttributes()
setParagraphAttributes()
JTextArea
insert()
append()
replaceRange()
相关问题
- java:自动调整JTable列的宽度javaswingjtabletablecolumn2021-01-11 22:58
- java:如何在Swing中为JTable提供分页支持?javaswingpaginationjtable2021-01-11 10:54
- java:从jar加载图像以进行Swing HTMLjavahtmlimageswing2021-01-11 04:58
- multithreading:如何从另一个线程暂停和恢复Java中的线程javamultithreadingswing2021-01-11 03:27
Google告诉我,至少那些方法是线程安全的.以下是链接再次断开的情况的概述:
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()