2

我试图了解 Handler、Thread、Future、Callable 等是如何工作的。

所以我写了这段代码来模拟最常见的任务类型:

    HandlerThread t1 = new HandlerThread("Thread di update");
    private ServiceHandler updateHandler;
    private static ExecutorService elaborateAll = Executors.newSingleThreadExecutor();
    private static Queue<Float> queue = new LinkedList<Float>();

    // ..................................... onCreate() and other code...

    @Override
    protected void onStart() {
            super.onStart();
            t1.start();
            updateHandler = new MyHandler(t1.getLooper());
    }

    private final static class MyHandler extends Handler {

            MyHandler(Looper loop) {
                    super(loop);
            }
            @Override
            public void handleMessage(Message msg) {

                    switch(msg.what) {
                            case MSG_UPDATE_POS:
                                    // EXECUTOR WITH CALLABLE AND FUTURE ===============================================
                                    CalculateSeparately calcola = new CalculateSeparately(msg.arg1);
                                    Future<Float> fuValore = elaborateAll.submit(calcola);
                                    float valore = fuValore.get();
                            break;
                            case MSG_DUE:
                                    // RUNNABLE AND THREAD ============================================================
                                    Runnable snd = new MioRunnnable(dato);
                                    Thread tSnd = new Thread(snd);
                                    tSnd.start();
                            break;
                            case MSG_TRE:
                                    // ASYNCTASK ====================================================================
                                    new MyAsyncTask().execute();
                            break;
                            case MSG_QUATTRO:
                                    // ASYNCTASK WITH GET ============================================================
                                    try {
                                            float mioValore = new ChiudiTraccia().execute().get();
                                    } catch (InterruptedException e) {
                                            e.printStackTrace();
                                    } catch (ExecutionException e) {
                                            e.printStackTrace();
                                    }
                            break;  
                            default:
                                    super.handleMessage(msg);                               
            }
    }

    public static class CalculateSeparately implements Callable<Float> {

            int val;

            public CalculateAvgSpeed(int media) {
                    this.val = media;
            }

            @Override
            public Float call() throws Exception {
                    if(queue.size() == 20)
                            queue.poll();
                    queue.offer(this.val);

                    float spd = calcolaMediaValCoda(coda);

                    return spd;
            }

    }

我已经测试过它向它发送消息并且一切正常。

那么我的代码是否正确(代码、Java 最佳实践等)?还是只是巧合,它起作用了?

先感谢您

4

0 回答 0