글
swt ui thread
If a listener has a large amount of work to perform, instead of performing that work in the UI-thread, it can fork a separate thread so the UI-thread can continue dispatching events. If the other thread needs to execute code that accesses an SWT object, such as changing the string in a label, there is a concurrency issue. At the very least, some kind of synchronization is necessary to prevent the operating system or SWT from crashing, hanging or behaving unpredictably.
SWT implements a single-threaded UI model often called apartment threading. In this model, only the UI-thread can invoke UI operations. SWT strictly enforces this rule. If you try and access an SWT object from outside the UI-thread, you get the exception "org.eclipse.swt.SWTException: Invalid thread access". Different operating systems have different rules governing threads, UI components and synchronization. Some use a single-threaded UI model like SWT. Others allow only one thread at a time in the window system library, controlling access through a global lock. This type of multi-threaded UI model is often called free threading. Currently, in order to be simple, efficient and portable, SWT is apartment threaded.
To allow background threads to perform operations on objects belonging to the UI-thread, the methods syncExec(Runnable runnable) and asyncExec(Runnable runnable) of Display are used. These are the only methods in SWT that can be called from any thread. They allow a runnable to be executed by the UI-thread, either synchronously, causing the background thread to wait for the runnable to finish, or asynchronously allowing the background thread to continue execution without waiting for the result. A runnable that is executed using syncExec() most closely matches the equivalent direct call to the UI operation because a Java method call always waits for the result before proceeding, just like syncExec().
The following code sets the text of a label from a background thread and waits for the operation to complete:
display.syncExec(
new Runnable() {
public void run(){
label.setText(text);
}
});
이 내용은 이클립스 faq에 있는 내용입니다.
http://www.eclipse.org/swt/faq.php#uithread
'JAVA' 카테고리의 다른 글
| swt ui thread (0) | 2011/12/30 |
|---|---|
| 이클립스 windowbuilder 프로젝트 import후 디자인 창 안보일 때 (0) | 2011/11/28 |
| jPasswordField는 getText가 되지 않습니다 (0) | 2010/11/12 |
| 자바에서 클래스관련 내용 (0) | 2010/04/15 |