Is there a way to prevent call() from returning value until for example a Boolean is set? So that i can control when futureCall.get() is done?
Main-class:
ExecutorService executor = Executors.newCachedThreadPool();
Future<List<Float>> futureCall = executor.submit((Callable<List<Float>>) new AxisMeasuring(2,100,this));
List<Float> jumpValues;
try {
jumpValues = futureCall.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
Callable-class:
public class AxisMeasuring implements SensorEventListener, Callable<List<Float>>{
AxisMeasuring(int _axis, final int _timeDelay, Context _context) {
axis = _axis;
final Context context = _context;
timeDelay = _timeDelay;
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
values.add(value);
if (!hadZeroValue && value <= 1) {
hadZeroValue = true;
}
if (hadZeroValue && value >= 12) {
Log.d("Debug","Point reached");
} else {
handler.postDelayed(runnable, timeDelay);
}
}
};
handler.post(runnable);
}
@Override
public List<Float> call() throws Exception {
return values;
}
}
futureCall.get() returns null instantly.
Yes, use a CountDownLatch
with count 1
.
CountDownLatch latch = new CountDownLatch(1);
and pass this latch to AxisMeasuring
:
public class AxisMeasuring implements SensorEventListener, Callable<List<Float>>{
private CountDownLatch latch;
AxisMeasuring(int _axis, final int _timeDelay, Context _context, CountDownLatch latch) {
latch = latch;
...
}
@Override
public List<Float> call() throws Exception {
latch.await(); // this will get blocked until you call latch.countDown after, for example, a Boolean is set
return values;
}
}
in other thread, you can call latch.countDown()
as signal.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.