interrupt work thread
This commit is contained in:
parent
ec55ccb1c6
commit
cb5b4665cf
@ -78,6 +78,9 @@ public enum CalculatorEngine {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private ThreadKiller threadKiller = new AndroidThreadKiller();
|
private ThreadKiller threadKiller = new AndroidThreadKiller();
|
||||||
|
|
||||||
|
// calculation thread timeout in milliseconds, after timeout thread would be interrupted
|
||||||
|
private int timeout = 2000;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public String format(@NotNull Double value) {
|
public String format(@NotNull Double value) {
|
||||||
return format(value, true);
|
return format(value, true);
|
||||||
@ -191,7 +194,7 @@ public enum CalculatorEngine {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
//Log.d(CalculatorEngine.class.getName(), "Main thread is waiting: " + Thread.currentThread().getName());
|
//Log.d(CalculatorEngine.class.getName(), "Main thread is waiting: " + Thread.currentThread().getName());
|
||||||
latch.await(2, TimeUnit.SECONDS);
|
latch.await(timeout, TimeUnit.MILLISECONDS);
|
||||||
//Log.d(CalculatorEngine.class.getName(), "Main thread got up: " + Thread.currentThread().getName());
|
//Log.d(CalculatorEngine.class.getName(), "Main thread got up: " + Thread.currentThread().getName());
|
||||||
|
|
||||||
final ParseException evalErrorLocal = exception.getObject();
|
final ParseException evalErrorLocal = exception.getObject();
|
||||||
@ -281,6 +284,11 @@ public enum CalculatorEngine {
|
|||||||
return varsRegister;
|
return varsRegister;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// for tests
|
||||||
|
void setTimeout(int timeout) {
|
||||||
|
this.timeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
/* private String commands(String str) {
|
/* private String commands(String str) {
|
||||||
return commands(str, false);
|
return commands(str, false);
|
||||||
}
|
}
|
||||||
|
@ -117,6 +117,14 @@ public class CalculatorEngineTest {
|
|||||||
junit.framework.Assert.fail();
|
junit.framework.Assert.fail();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
cm.setTimeout(5000);
|
||||||
|
Assert.assertEquals("2", cm.evaluate(JsclOperation.numeric, "2!").getResult());
|
||||||
|
} finally {
|
||||||
|
cm.setTimeout(2000);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user