Skip to content

Commit 7576ff7

Browse files
committed
Using what we learned in SwtExecProfile, immediate(), async(), and blocking() should all be about 30% faster now.
1 parent 795a8e6 commit 7576ff7

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

src/com/diffplug/common/swt/SwtExec.java

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,11 @@ public static SwtExec immediate() {
8989
immediate = new SwtExec(Display.getDefault()) {
9090
@Override
9191
public void execute(Runnable runnable) {
92-
requireNonNull(runnable);
93-
if (!display.isDisposed()) {
94-
if (Thread.currentThread() == display.getThread()) {
95-
runnable.run();
96-
} else {
97-
display.asyncExec(runnable);
98-
}
92+
if (Thread.currentThread() == swtThread) {
93+
runnable.run();
9994
} else {
100-
throw new RejectedExecutionException();
95+
requireNonNull(runnable);
96+
display.asyncExec(runnable);
10197
}
10298
}
10399
};
@@ -146,15 +142,11 @@ private Blocking() {
146142

147143
@Override
148144
public void execute(Runnable runnable) {
149-
requireNonNull(runnable);
150-
if (!display.isDisposed()) {
151-
if (Thread.currentThread() == display.getThread()) {
152-
runnable.run();
153-
} else {
154-
display.syncExec(runnable);
155-
}
145+
if (Thread.currentThread() == swtThread) {
146+
runnable.run();
156147
} else {
157-
throw new RejectedExecutionException();
148+
requireNonNull(runnable);
149+
display.syncExec(runnable);
158150
}
159151
}
160152

@@ -165,7 +157,7 @@ public void execute(Runnable runnable) {
165157
* @return the value which was returned by supplier.
166158
*/
167159
public <T> T get(Supplier<T> supplier) {
168-
if (Thread.currentThread() == display.getThread()) {
160+
if (Thread.currentThread() == swtThread) {
169161
return supplier.get();
170162
} else {
171163
Nullable<T> holder = Nullable.ofVolatileNull();
@@ -317,6 +309,7 @@ private Subscription subscribe(Supplier<Subscription> subscriber) {
317309
}
318310

319311
protected final Display display;
312+
protected final Thread swtThread;
320313
protected final Rx.RxExecutor rxExecutor;
321314

322315
/** Returns an instance of {@link com.diffplug.common.rx.Rx.RxExecutor}. */
@@ -327,11 +320,13 @@ public Rx.RxExecutor getRxExecutor() {
327320

328321
SwtExec(Display display) {
329322
this.display = display;
323+
this.swtThread = display.getThread();
330324
this.rxExecutor = Rx.on(this, new SwtScheduler(this));
331325
}
332326

333327
SwtExec(Display display, Rx.RxExecutor rxExecutor) {
334328
this.display = display;
329+
this.swtThread = display.getThread();
335330
this.rxExecutor = rxExecutor;
336331
}
337332

@@ -354,11 +349,7 @@ public Rx.RxExecutor getRxExecutor() {
354349
@Override
355350
public void execute(Runnable runnable) {
356351
requireNonNull(runnable);
357-
if (!display.isDisposed()) {
358-
display.asyncExec(runnable);
359-
} else {
360-
throw new RejectedExecutionException();
361-
}
352+
display.asyncExec(runnable);
362353
}
363354

364355
////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)