Skip to content

Commit 2753f76

Browse files
committed
#2, #3 Improve thread and exception handling
1 parent a861310 commit 2753f76

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ to pool size of 25 threads. You can change that with `AsyncManager.getInstance()
9898
<dependency>
9999
<groupId>org.vaadin.helper</groupId>
100100
<artifactId>async-manager</artifactId>
101-
<version>1.0.0-beta1</version>
101+
<version>1.0.0-beta2</version>
102102
</dependency>
103103
```
104104

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>org.vaadin.helper</groupId>
77
<artifactId>async-manager</artifactId>
8-
<version>1.0.0-beta1</version>
8+
<version>1.0.0-beta2</version>
99
<name>Async Manager</name>
1010
<description>Async Manager for Vaadin Flow</description>
1111

src/main/java/org/vaadin/flow/helper/AsyncTask.java

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public class AsyncTask {
5959
* push is used for current task
6060
*/
6161
private AtomicInteger missedPolls = new AtomicInteger();
62+
/**
63+
* {@code true}, if thread may be interrupted if UI/Component detaches
64+
*/
65+
private boolean mayInterrupt = true;
6266

6367
/**
6468
* Create a new task
@@ -78,33 +82,31 @@ public void push(Command command) {
7882
if (parentUI == null) {
7983
return;
8084
}
81-
if (missedPolls.get() == PUSH_ACTIVE && parentUI.getPushConfiguration().getPushMode() == PushMode.MANUAL) {
82-
parentUI.accessSynchronously(() -> {
83-
try {
84-
command.execute();
85+
boolean mustPush = missedPolls.get() == PUSH_ACTIVE && parentUI.getPushConfiguration().getPushMode() == PushMode.MANUAL;
86+
parentUI.accessSynchronously(() -> {
87+
try {
88+
command.execute();
89+
if (mustPush) {
8590
parentUI.push();
86-
} catch (UIDetachedException ignore) {
87-
// Do not report
88-
// How could this even happen?
89-
} catch (Exception e) {
90-
// Dump
91-
asyncManager.handleException(this, e);
9291
}
93-
});
94-
} else {
95-
// Automatic -- changes will be pushed automatically
96-
// Disabled -- we're using polling and this is called
97-
// within UIDLRequestHandler
98-
parentUI.accessSynchronously(command);
99-
}
92+
} catch (UIDetachedException ignore) {
93+
// Do not report
94+
// How could this even happen?
95+
} catch (Exception e) {
96+
// Dump
97+
asyncManager.handleException(this, e);
98+
}
99+
});
100100
}
101101

102102
/**
103-
* Cancel and unregister the task
103+
* Cancel and unregister the task. Thread interruption behaviour is controlled
104+
* by {@link AsyncTask#allowThreadInterrupt()} and
105+
* {@link AsyncTask#preventThreadInterrupt()} methods.
104106
*/
105107
public void cancel() {
106108
if (!task.isCancelled() && !task.isDone()) {
107-
task.cancel(true);
109+
task.cancel(mayInterrupt);
108110
}
109111
remove();
110112
}
@@ -129,6 +131,20 @@ public void await() throws ExecutionException, InterruptedException {
129131
task.get();
130132
}
131133

134+
/**
135+
* Allow worker thread to be interrupted when UI or Component detaches. Default behaviour.
136+
*/
137+
public void allowThreadInterrupt() {
138+
this.mayInterrupt = true;
139+
}
140+
141+
/**
142+
* Prevent worker thread interruption when UI or Component detaches.
143+
*/
144+
public void preventThreadInterrupt() {
145+
this.mayInterrupt = false;
146+
}
147+
132148
//--- Implementation
133149

134150
/**

0 commit comments

Comments
 (0)