第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Callable 如何防止 call() 返回值

Callable 如何防止 call() 返回值

寶慕林4294392 2021-08-25 16:17:16
有沒有辦法防止 call() 在例如設(shè)置布爾值之前返回值?這樣我就可以控制futureCall.get()何時(shí)完成?主類: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();}可調(diào)用類: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() 立即返回 null。
查看完整描述

1 回答

?
慕神8447489

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊

是的,將 aCountDownLatch與 count 一起使用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()作為信號(hào)調(diào)用。


查看完整回答
反對(duì) 回復(fù) 2021-08-25
  • 1 回答
  • 0 關(guān)注
  • 235 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)