Callable 如何防止 call() 返回值
Callable How to prevent call() from returning value
有没有办法阻止 call() 在设置布尔值之前返回值?这样我就可以控制 futureCall.get() 何时完成?
主要-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();
}
可调用-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 立即无效。
是,使用 CountDownLatch
计数 1
。
CountDownLatch latch = new CountDownLatch(1);
并将此锁存器传递给 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;
}
}
在其他线程中,你可以调用latch.countDown()
作为信号。
有没有办法阻止 call() 在设置布尔值之前返回值?这样我就可以控制 futureCall.get() 何时完成?
主要-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();
}
可调用-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 立即无效。
是,使用 CountDownLatch
计数 1
。
CountDownLatch latch = new CountDownLatch(1);
并将此锁存器传递给 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;
}
}
在其他线程中,你可以调用latch.countDown()
作为信号。