interrupt work thread

This commit is contained in:
serso 2011-10-28 00:41:03 +04:00
parent 1035922649
commit 53476b064a
2 changed files with 38 additions and 22 deletions

View File

@ -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);
} }

View File

@ -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