summaryrefslogtreecommitdiffstats
path: root/examples/java/AsyncTinyB.java
diff options
context:
space:
mode:
authorPetre Eftime <[email protected]>2016-07-04 17:28:33 +0300
committerPetre Eftime <[email protected]>2016-07-05 15:26:14 +0300
commit3e629a23b367dd2c6a4b0c5d6ec970f6278805c1 (patch)
treed56a8893bd0f824df9672becf5bebf9d243e1bb7 /examples/java/AsyncTinyB.java
parent2ff15f0bd3fd55f458b5de9d7be25186d39dae4d (diff)
examples: java: Improve interrupt handling in Java examples
Signed-off-by: Petre Eftime <[email protected]>
Diffstat (limited to 'examples/java/AsyncTinyB.java')
-rw-r--r--examples/java/AsyncTinyB.java22
1 files changed, 18 insertions, 4 deletions
diff --git a/examples/java/AsyncTinyB.java b/examples/java/AsyncTinyB.java
index 2f1a0254..b566b795 100644
--- a/examples/java/AsyncTinyB.java
+++ b/examples/java/AsyncTinyB.java
@@ -1,6 +1,8 @@
import tinyb.*;
import java.util.*;
import java.time.*;
+import java.util.concurrent.locks.*;
+import java.util.concurrent.TimeUnit;
public class AsyncTinyB {
private static final float SCALE_LSB = 0.03125f;
@@ -57,7 +59,7 @@ public class AsyncTinyB {
/*
* After we find the device we can stop looking for other devices.
*/
- manager.stopDiscovery();
+ //manager.stopDiscovery();
if (sensor == null) {
System.err.println("No sensor found with the provided address.");
@@ -74,11 +76,18 @@ public class AsyncTinyB {
System.exit(-1);
}
- final Thread mainThread = Thread.currentThread();
+ Lock lock = new ReentrantLock();
+ Condition cv = lock.newCondition();
+
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
running = false;
- sensor.disconnect();
+ lock.lock();
+ try {
+ cv.signalAll();
+ } finally {
+ lock.unlock();
+ }
}
});
@@ -142,7 +151,12 @@ public class AsyncTinyB {
System.out.println(
String.format(" Temp: Object = %fC, Ambient = %fC", objectTempCelsius, ambientTempCelsius));
- Thread.sleep(1000);
+ lock.lock();
+ try {
+ cv.await(1, TimeUnit.SECONDS);
+ } finally {
+ lock.unlock();
+ }
}
sensor.disconnect();