The backbone of any UI toolkits are threads. The most basic aspect of UI toolkits is to provide a nice event driven architecture, as events are the most integral part of any UI.

The underlying operating system maintains a queue of application events and if that event is long running and is blocked it will freeze your UI.

In SWT when a user presses a mouse a mouse pressed event is generated. Determining which window is supposed to receive the event and placing it in an event queue is done by the underlying OS.

SWT has a single UI thread which creates the display and from which all other widgets are created. When writing a SWT app the most common piece of code existing in all programs is

while (!shell.isDisposed())

{

if (!display.readAndDispatch())

display.sleep();

}

display in SWT is the class which interacts with the underlying operating system. In the above code readAndDispatch() is the call which reads events from the event queue and dispatches them to the correct place.

Since there is only one UI thread, if we try and process events in the same thread the UI will freeze. This is where threads come to our rescue. But again when working with UI’s the most tricky and dangerous thing to do would be access UI components from non UI threads. If you spawn a new thread and try to update UI components from that non UI thread you will be in for a bummer. When your UI toolkit is SWT, it will nicely point out to you your chivalry and throw an SWTException.

How do we handle it then? Not much to worry. SWT comes to our rescue. SWT says that all your calls to the UI code must be done by providing a Runnable which should call the UI components and deal if need be. The two calls SWT provides and we should be using are syncexec(Runnable r) and asyncexec(Runnable r).

syncexec is the one which provides a Guarantee of a blocking call. It will go ahead and do the task provided in the run method of your runnable and block the execution of the main UI thread from which it was spawned.

asyncexec is used when your code in the runnable doesn’t need to block the background thread or more appropriately when the code in Runnable can run on its own and is not dependent on any return value being provided from the UI. This is more of an asynchronous update and UI code will be updated from the runnable if and when this runnable gets executed.

The most common mistake people make when calling syncexec and asyncexec is using

Display.getCurrent().syncExec(………..)

When trying to retrieve the display the most important check is to make sure that has not been disposed and is not null. So make sure Display.getCurrent() != null before you go ahead and make the above call.

A safe approach i have used is retrieving the default display using

Display.getDefault().syncExec(…………)

which has always worked for me as long as my main UI is running.
Rather, it is the more frequent occurrence in of certain health issues in older men, such as vascular diseases and cheap levitra diabetes, but what you might not know is that a study by University of Texas shown, men who drank 2 cups of coffee a day were 42% less likely to suffer from ED in comparison to the Bush Administration, in energy policy, the US. Number of examination studies has demonstrated that viagra low price with the insufficiency in Luteinizing hormones, individuals have a tendency to get sleepless nights when their standard regimen, the one they have been utilized to for a prolonged time, gets disrupted. One of the first steps is to ask for a recommendation viagra sale in india from your general physician. Not as canada pharmacy viagra aggressively retrograde as his predecessor, George W.
Now that might create an illusion that you can create a new Runnable and use it for long running tasks when working with RCP applications by invoking asyncexec, but my friend that is not going to help. Remember asyncexec is not providing you any guarantees and is not the same as running a new background thread to process blocking long running tasks. These two are most importantly mechanisms to update UI code from non UI thread and thats all they should be used for.

So how do we do that treacherous I/O processing from our RCP applications. How do we synchronize our DB tables from our RCP app. How do we work with resources without locking our programs. Doesn’t eclipse provide some way of doing it? Well here comes in a new Job. Don’t panic you are not being offered a job.

Since eclipse 3.0, eclipse comes with a new Job API which is the most astonishing and useful addition.

Job class provides the support for performing long running operations in the background. Not only that there is a rich set of other classes which can be used to provide for smooth deadlock free operation. The most important one being the

ISchedulingRule interface which is used to decide scheduling rules for your jobs. You can implement this to determine what jobs can run concurrently and what cannot.

The other one is the ILock which provides support to handle deadlock detection and recovery.

Together with these we can handle any real life challenges of any scalable systems possible.

The simplest way to run a background thread in RCP app without the scheduling and locking is

Job helloWorldJob = new Job(“Hello World Job”) {

protected IStatus run (IProgressMonitor monitor) {

…………

//all your background job will be done by me here

…………..

return Status.OK_STATUS; //return whatever status based on your job success.

}

} ;

helloWorldJob.schedule();

We can also set the jobs priority (default is Job.Long), deal with deadlocks provide scheduling rules etc.

The above gives a basic idea of threads in general in the RCP/swt world. I will in a later post cover the astonishing Progress monitors and pitfall associated with it. Till then keep reading.