Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Threading strategies in RCP(One Thread with massive Display.syncExec or Threads for every task where SWT is not concerned?)
Threading strategies in RCP [message #1017791] Tue, 12 March 2013 10:48 Go to next message
Simon Scholz is currently offline Simon Scholz
Messages: 73
Registered: April 2012
Location: Germany
Member
Hello,

I have an org.eclipse.ui.ISelectionListener, which does some non ui processes and afterwards it builds the UI according to the ISelection.

The Problem is when the user for instance navigates a tree with the cursor keys the selectionChanged event is fired a couple of times and the UI hangs up for a few seconds, because of the processes in the ISelectionListener.

Therefore I wanted to use a delay, until the processes will start and abort the processing, if the selection has changed meanwhile.

This works great:


    @Override
    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
        // wait half a second and then check if threadId is still current to trigger the actual process
        DelayedSelectionChanged delayedSelectionChanged = new DelayedSelectionChanged(
                this, threadId, part, selection);
        executorService.schedule(delayedSelectionChanged, 500,
                TimeUnit.MILLISECONDS);

    }


Of cause in between "Invalid Thread Access Exceptions" are thrown.

What would you suggest?

Should I directly use the Display.syncExec() method after the half second to avoid these Exceptions and then again build more threads for the processing?
Should I call every time SWT is used within the Process Display.syncExec()?

I mean how performance consuming is it, when you pass several Runnables to the Display.syncExec() during the process?

Best regards,

Simon
Re: Threading strategies in RCP [message #1017817 is a reply to message #1017791] Tue, 12 March 2013 11:40 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel Krügler
Messages: 853
Registered: July 2009
Senior Member
On 2013-03-12 15:48, Simon SSC wrote:
> Hello,
>
> I have an org.eclipse.ui.ISelectionListener, which does some non ui
> processes and afterwards it builds the UI according to the ISelection.
>
> The Problem is when the user for instance navigates a tree with the
> cursor keys the selectionChanged event is fired a couple of times and
> the UI hangs up for a few seconds, because of the processes in the
> ISelectionListener.
>
> Therefore I wanted to use a delay, until the processes will start and
> abort the processing, if the selection has changed meanwhile.

Understandable.

> This works great:
>
> @Override
> public void selectionChanged(IWorkbenchPart part, ISelection
> selection) {
> // wait half a second and then check if threadId is still
> current to trigger the actual process
> DelayedSelectionChanged delayedSelectionChanged = new
> DelayedSelectionChanged(
> this, threadId, part, selection);
> executorService.schedule(delayedSelectionChanged, 500,
> TimeUnit.MILLISECONDS);
> }
>
>
> Of cause in between "Invalid Thread Access Exceptions" are thrown.
>
> What would you suggest?

I have no idea what the constructor of DelayedSelectionChanged does, so
my advice can only be limited.

> Should I directly use the Display.syncExec() method after the half
> second to avoid these Exceptions and then again build more threads for
> the processing?

I don't see any reason for any synchronization with the UI thread
between the calls, (but I also don't know precisely what the first call
does).

> Should I call every time SWT is used within the Process Display.syncExec()?

You mean thread, not process, right? I would always call
Display.asyncExec() within the non-UI thread. Display.syncExec() is
generally susceptible to dead-locks and I stay away from it if possible.
I also would try to minimize the number of thread switches within the
delayed worker thread.

HTH & Greetings from Bremen,

Daniel Krügler
Re: Threading strategies in RCP [message #1017853 is a reply to message #1017817] Tue, 12 March 2013 12:30 Go to previous messageGo to next message
Simon Scholz is currently offline Simon Scholz
Messages: 73
Registered: April 2012
Location: Germany
Member
Hi Daniel,

Thanks for the reply.
The Runnable for the delay looks like this:

public class DelayedSelectionChanged implements Runnable {

    private final IDelayedSelectionListener propertiesView;

    private final AtomicLong currentId;

    private final long id;

    private final IWorkbenchPart part;

    private final ISelection selection;

    /**
     * Constructor.
     * 
     * @param propertiesView
     *            {@link PropertiesView}
     * @param currentId
     *            {@link AtomicLong}
     * @param part
     *            {@link IWorkbenchPart}
     * @param selection
     *            {@link ISelection}
     */
    public DelayedSelectionChanged(IDelayedSelectionListener propertiesView,
            AtomicLong currentId, IWorkbenchPart part, ISelection selection) {
        this.propertiesView = propertiesView;
        this.currentId = currentId;
        this.part = part;
        this.selection = selection;
        id = currentId.incrementAndGet();
    }

    @Override
    public void run() {
        if (this.id == currentId.get()) {
            propertiesView.delayedSelectionChanged(part, selection);
        }
    }
}


Quote:
I also would try to minimize the number of thread switches within the
delayed worker thread.


Do you have a better idea than my approach with the DelayedSelectionChanged?

Best regards,

Simon

P.S.: Ich bin auch manchmal in Bremen und habe dort vor 2 Jahren mal ein Praktikum gemacht. Smile
Re: Threading strategies in RCP [message #1021913 is a reply to message #1017791] Wed, 20 March 2013 19:12 Go to previous message
Wim Jongman is currently offline Wim Jongman
Messages: 418
Registered: July 2009
Senior Member
Hi Simon,

You can register your listener with the AddPostSelectionListener method. This
has the delay build in.

Best Regards,

Wim


> Hello,
>
> I have an org.eclipse.ui.ISelectionListener, which does some non ui
processes and afterwards it builds the UI according to the ISelection.
>
> The Problem is when the user for instance navigates a tree with the cursor
keys the selectionChanged event is fired a couple of times and the UI hangs
up for a few seconds, because of the processes in the ISelectionListener.
>
> Therefore I wanted to use a delay, until the processes will start and abort
the processing, if the selection has changed meanwhile.
>
> This works great:
>
>
>
> @Override
> public void selectionChanged(IWorkbenchPart part, ISelection selection) {
> // wait half a second and then check if threadId is still current to
trigger the actual process
> DelayedSelectionChanged delayedSelectionChanged = new
DelayedSelectionChanged(
> this, threadId, part, selection);
> executorService.schedule(delayedSelectionChanged, 500,
> TimeUnit.MILLISECONDS);
>
> }
>
>
> Of cause in between "Invalid Thread Access Exceptions" are thrown.
>
> What would you suggest?
>
> Should I directly use the Display.syncExec() method after the half second
to avoid these Exceptions and then again build more threads for the
processing?
> Should I call every time SWT is used within the Process Display.syncExec()?
>
> I mean how performance consuming is it, when you pass several Runnables to
the Display.syncExec() during the process?
>
> Best regards,
>
> Simon
Previous Topic:Toolbar pulldown - item order
Next Topic:Authentication
Goto Forum:
  


Current Time: Mon Jul 28 22:42:59 EDT 2014

Powered by FUDForum. Page generated in 0.03653 seconds