根据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方法似乎很少见,因此這樣的列表不能太长...)
- 1月前1 #
- 1月前2 #
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:
類是執行緒安全的 - 1月前3 #
有關在javadocs&src檔案中帶有註釋的類的列表,"是執行緒安全的" 返迴以下內容
JEditorPane JTextArea AbstractDocument DefaultCaret DefaultStyledDocument JTextComponent PlainDocument StyleContext HTMLDocument UndoManager
這並不是說src中還有其他檔案記錄或未記錄的執行緒安全記錄。
這让我感到很奇怪,但我会將大多陣列件视為 not 具有執行緒安全性,並且由於Swing是單執行緒模型,並且所有更新都需要在事件分派器執行緒上进行,因此這非常容易做到.
- 1月前4 #
但是您已经有了答案:只有那些方法,它们是 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至關重要
- 1月前5 #
在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()