Find out if the calling thread is the SWT UI thread - determine the calling thread
Asked Answered
R

2

7

I have this module that's being used in multiple parts of the application COMM (on the SWT Ui side , on the backend etc). This module has a method sendMessage in which I want to add a routine to determine if the calling thread (just in case of using this in the UI) is the SWT UI thread. And warn the programmer that he is trying to do a time consuming operation from the UI thread ... which is bad :)

I want to do this ofcourse by not adding any dependencies on the UI module (from COMM).

How can I determine if the calling thread is the SWT UI thread ?

Thanks, Mircea

Rosetta answered 14/8, 2012 at 11:58 Comment(1)
The problem with this is, that you want that at compile time, but not at runtime. So it should not be part of your library. Maybe use findbugs and some annotation?Hinshaw
C
10

You can call Display.getThread() to get the current UI thread for your application.

If you don't want to take dependencies on SWT UI, then you'll have to use reflection. For example:

public static boolean isUIThread()
{
    Object uiThread = null;

    try
    {
        Class displayClass = Class.forName("org.eclipse.swt.widgets.Display");
        Method getDefaultMethod = displayClass.getDeclaredMethod("getDefault", new Class[] { });
        Object display = getDefaultMethod.invoke(null, new Object[] { });

        Method getThreadMethod = displayClass.getDeclaredMethod("getThread", new Class[] { });
        uiThread = getThreadMethod.invoke(display, new Object[] { });
    }
    catch(Exception e)
    {
        log.warn("Could not determine UI thread using reflection", e);
    }

    return (Thread.currentThread() == uiThread);
}
Charlie answered 14/8, 2012 at 13:28 Comment(4)
Note: you may need to also add a DynamicImport-Package clause to your MANIFEST.MF in order for the reflection to be successful. I needed to do: DynamicImport-Package: org.eclipse.swt before I could successfully do the Class.forName on the swt classes.Prehension
Since it's possible to have multiple Displays in a single application, a minor improvement would be to check that the static method org.eclipse.swt.widgets.Display.getCurrent() returns null, instead of checking Display.getDefault().getThread(). +1 for the helpful answer though!Diadem
@Diadem While SWT supports multiple displays on Windows, that is the only platform that supports multiple displays. If you are working on a project that is expected to build and run on Mac or Linux (such as Eclipse itself), it should be safe to assume there is only one display. Unless your application is doing so much painting of so many windows on so many monitors that it is impossible to push that many pixels from a single core, the use of multiple displays is a design flaw that limits portability (and/or permits bad practices like running long jobs on a UI thread), not an asset.Oceanid
@TheodoreMurdock There's actually another case - I've done development with RAP, which also uses multiple displays in a single application. But a good clarification, thanks.Diadem
S
0

I belive this code will determine, in runtime, if the current thread is the UI thread in SWT. It is basically the same answer as previously added without using reflection.

if(Thread.currentThread() == Display.getDefault().getThread())

Subservience answered 13/12, 2018 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.