Skip to content

Commit b0184e6

Browse files
committed
fixed fx task executor.
1 parent a96e328 commit b0184e6

File tree

4 files changed

+43
-62
lines changed

4 files changed

+43
-62
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ ext.jme3_xbuf_version = '0.9.1'
6161
ext.junitPlatformVersion = "1.0.0"
6262
ext.junitJupiterVersion = "5.0.0"
6363
ext.log4jVersion = '2.6.2'
64-
ext.bintrayVersion = version + '-3'
64+
ext.bintrayVersion = version + '-4'
6565

6666
junitPlatform {
6767
filters {
@@ -128,7 +128,7 @@ dependencies {
128128
compile 'org.fxmisc.richtext:richtextfx:0.8.2'
129129
compile 'org.controlsfx:controlsfx:9.0.0'
130130
compile 'com.spaceshift:rlib.fx:4.2.1-Final'
131-
compile 'com.spaceshift:rlib:6.8.3-Final'
131+
compile 'com.spaceshift:rlib:6.8.5-Final'
132132
compile ('com.jme3x:jme-jfx:1.7.5-Final') {
133133
exclude group: 'org.jmonkeyengine'
134134
}

src/main/java/com/ss/editor/executor/impl/FxEditorTaskExecutor.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,38 @@ public FxEditorTaskExecutor() {
2828
setPriority(NORM_PRIORITY);
2929
try {
3030
Platform.startup(this::start);
31-
} catch (final IllegalStateException e) {
31+
} catch (IllegalStateException e) {
3232
start();
3333
}
3434
}
3535

3636
@Override
3737
@FxThread
38-
protected void doExecute(@NotNull final Array<Runnable> execute, @NotNull final Array<Runnable> executed) {
38+
protected void doExecute(@NotNull Array<Runnable> execute, @NotNull Array<Runnable> executed) {
3939

40-
final Runnable[] array = execute.array();
40+
var array = execute.array();
4141

4242
for (int i = 0, length = execute.size(); i < length; ) {
4343
try {
4444

4545
for (int count = 0; count < EXECUTE_LIMIT && i < length; count++, i++) {
4646

47-
final Runnable task = array[i];
47+
var task = array[i];
4848
try {
4949
task.run();
50-
} catch (final Exception e) {
50+
} catch (Exception e) {
5151
EditorUtil.handleException(LOGGER, this, e);
5252
}
5353

5454
executed.add(task);
5555
}
5656

57-
} catch (final Exception e) {
57+
} catch (Exception e) {
5858
LOGGER.warning(e);
5959
}
6060
}
6161

62-
ConcurrentUtils.notifyAll(this);
62+
ConcurrentUtils.notifyAll(fxTask);
6363
}
6464

6565
@Override
@@ -111,9 +111,9 @@ public void run() {
111111

112112
@FromAnyThread
113113
private void executeInFxUiThread() {
114-
synchronized (this) {
114+
synchronized (fxTask) {
115115
Platform.runLater(fxTask);
116-
ConcurrentUtils.waitInSynchronize(this);
116+
ConcurrentUtils.waitInSynchronize(fxTask);
117117
}
118118
}
119119
}

src/main/java/com/ss/editor/manager/ExecutorManager.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,7 @@ public class ExecutorManager {
3333
@Nullable
3434
private static ExecutorManager instance;
3535

36-
/**
37-
* Gets instance.
38-
*
39-
* @return the instance
40-
*/
41-
@NotNull
42-
public static ExecutorManager getInstance() {
36+
public static @NotNull ExecutorManager getInstance() {
4337
if (instance == null) instance = new ExecutorManager();
4438
return instance;
4539
}
@@ -99,10 +93,10 @@ private ExecutorManager() {
9993
@FromAnyThread
10094
public void addBackgroundTask(@NotNull final Runnable task) {
10195

102-
final EditorTaskExecutor[] executors = getBackgroundTaskExecutors();
103-
final AtomicInteger nextTaskExecutor = getNextBackgroundTaskExecutor();
96+
var executors = getBackgroundTaskExecutors();
97+
var nextTaskExecutor = getNextBackgroundTaskExecutor();
10498

105-
final int index = nextTaskExecutor.incrementAndGet();
99+
var index = nextTaskExecutor.incrementAndGet();
106100

107101
if (index < executors.length) {
108102
executors[index].execute(task);
@@ -113,14 +107,13 @@ public void addBackgroundTask(@NotNull final Runnable task) {
113107
}
114108

115109
/**
116-
* Add a new javaFX task.
110+
* Add the new task to be executed in the JavaFX thread.
117111
*
118-
* @param task the javaFX task.
112+
* @param task the task.
119113
*/
120114
@FromAnyThread
121-
public void addFxTask(@NotNull final Runnable task) {
122-
final EditorTaskExecutor executor = getFxTaskExecutor();
123-
executor.execute(task);
115+
public void addFxTask(@NotNull Runnable task) {
116+
getFxTaskExecutor().execute(task);
124117
}
125118

126119
/**
@@ -129,12 +122,13 @@ public void addFxTask(@NotNull final Runnable task) {
129122
* @param task the editor task.
130123
*/
131124
@FromAnyThread
132-
public void addJmeTask(@NotNull final Runnable task) {
133-
final JmeThreadExecutor executor = getJmeTasksExecutor();
134-
executor.addToExecute(task);
125+
public void addJmeTask(@NotNull Runnable task) {
126+
getJmeTasksExecutor().addToExecute(task);
135127
}
136128

137129
/**
130+
* Get the list of background tasks executors.
131+
*
138132
* @return the list of background tasks executors.
139133
*/
140134
@FromAnyThread
@@ -143,6 +137,8 @@ public void addJmeTask(@NotNull final Runnable task) {
143137
}
144138

145139
/**
140+
* Get the executor of javaFX tasks.
141+
*
146142
* @return the executor of javaFX tasks.
147143
*/
148144
@FromAnyThread
@@ -151,6 +147,8 @@ public void addJmeTask(@NotNull final Runnable task) {
151147
}
152148

153149
/**
150+
* Get the index of a next background executor.
151+
*
154152
* @return the index of a next background executor.
155153
*/
156154
@FromAnyThread
@@ -159,7 +157,9 @@ public void addJmeTask(@NotNull final Runnable task) {
159157
}
160158

161159
/**
162-
* @return the executor of editor tasks.
160+
* Get the executor of jME tasks.
161+
*
162+
* @return the executor of jME tasks.
163163
*/
164164
@FromAnyThread
165165
private @NotNull JmeThreadExecutor getJmeTasksExecutor() {
@@ -173,7 +173,7 @@ public void addJmeTask(@NotNull final Runnable task) {
173173
* @param timeout the timeout.
174174
*/
175175
@FromAnyThread
176-
public void schedule(@NotNull final Runnable runnable, final long timeout) {
176+
public void schedule(@NotNull Runnable runnable, long timeout) {
177177
scheduledExecutorService.schedule(runnable, timeout, TimeUnit.MILLISECONDS);
178178
}
179179

@@ -184,7 +184,7 @@ public void schedule(@NotNull final Runnable runnable, final long timeout) {
184184
* @param delay the delay.
185185
*/
186186
@FromAnyThread
187-
public void scheduleAtFixedRate(@NotNull final Runnable runnable, final long delay) {
187+
public void scheduleAtFixedRate(@NotNull Runnable runnable, long delay) {
188188
scheduledExecutorService.scheduleAtFixedRate(runnable, delay, delay, TimeUnit.MILLISECONDS);
189189
}
190190
}

src/main/java/com/ss/editor/ui/component/asset/tree/ResourceTree.java

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import com.ss.editor.ui.component.asset.tree.context.menu.action.*;
1414
import com.ss.editor.ui.component.asset.tree.resource.*;
1515
import com.ss.editor.ui.util.UiUtils;
16+
import com.ss.rlib.concurrent.barrier.Barrier;
17+
import com.ss.rlib.concurrent.barrier.BarrierFactory;
1618
import com.ss.rlib.function.IntObjectConsumer;
1719
import com.ss.rlib.util.StringUtils;
1820
import com.ss.rlib.util.array.Array;
@@ -125,6 +127,12 @@ private static int getLevel(@Nullable ResourceElement element) {
125127
@Nullable
126128
private final Consumer<ResourceElement> openFunction;
127129

130+
/**
131+
* The memory barrier.
132+
*/
133+
@NotNull
134+
private final Barrier barrier;
135+
128136
/**
129137
* The action tester.
130138
*/
@@ -149,12 +157,6 @@ private static int getLevel(@Nullable ResourceElement element) {
149157
@Nullable
150158
private IntObjectConsumer<ResourceTree> expandHandler;
151159

152-
/**
153-
* Barrier.
154-
*/
155-
private volatile int barrier;
156-
private int barrierSinck;
157-
158160
/**
159161
* The flag of read only mode.
160162
*/
@@ -186,6 +188,7 @@ public ResourceTree(@Nullable Consumer<ResourceElement> openFunction, boolean re
186188
this.selectedElements = ArrayFactory.newConcurrentAtomicARSWLockArray(ResourceElement.class);
187189
this.extensionFilter = ArrayFactory.newArray(String.class, 0);
188190
this.actionTester = actionClass -> true;
191+
this.barrier = BarrierFactory.newVolatileBased();
189192

190193
expandedItemCountProperty()
191194
.addListener((observable, oldValue, newValue) -> processChangedExpands(newValue));
@@ -199,22 +202,6 @@ public ResourceTree(@Nullable Consumer<ResourceElement> openFunction, boolean re
199202
setFocusTraversable(true);
200203
}
201204

202-
/**
203-
* Read barrier.
204-
*/
205-
@FromAnyThread
206-
protected void readBarrier() {
207-
barrierSinck = barrier + 1;
208-
}
209-
210-
/**
211-
* Write barrier.
212-
*/
213-
@FromAnyThread
214-
protected void writeBarrier() {
215-
barrier = barrierSinck + 1;
216-
}
217-
218205
/**
219206
* Set true if need to use lazy mode.
220207
*
@@ -509,6 +496,7 @@ public void fill(@NotNull Array<Path> rootFolders) {
509496
/**
510497
* Prepare this component to fill again.
511498
*/
499+
@FxThread
512500
protected void prepareToFill() {
513501

514502
var onLoadHandler = getOnLoadHandler();
@@ -628,7 +616,6 @@ private void showLoading() {
628616
@BackgroundThread
629617
private void startBackgroundFill(@NotNull Path path) {
630618

631-
var nextBarrier = barrier + 1;
632619
var rootElement = createFor(path);
633620
var newRoot = new TreeItem<ResourceElement>(rootElement);
634621
newRoot.setExpanded(true);
@@ -639,8 +626,6 @@ private void startBackgroundFill(@NotNull Path path) {
639626
cleanup(newRoot);
640627
}
641628

642-
barrier = nextBarrier;
643-
644629
EXECUTOR_MANAGER.addFxTask(() -> applyNewRoot(newRoot));
645630
}
646631

@@ -649,19 +634,15 @@ private void startBackgroundFill(@NotNull Path path) {
649634
*
650635
* @param newRoot the new root,
651636
*/
652-
@BackgroundThread
637+
@FxThread
653638
private void applyNewRoot(@NotNull TreeItem<ResourceElement> newRoot) {
654639

655-
var nextBarrier = barrier + 1;
656-
657640
setRoot(newRoot);
658641

659642
var onLoadHandler = getOnLoadHandler();
660643
if (onLoadHandler != null) {
661644
onLoadHandler.accept(Boolean.TRUE);
662645
}
663-
664-
barrier = nextBarrier;
665646
}
666647

667648
/**

0 commit comments

Comments
 (0)