android处理程序和线程

o0lyfsai  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(139)

我试图理解处理程序、线程、未来、可调用等是如何工作的。
所以我写了这段代码来模拟最常见的任务类型:

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最佳实践等)?或者只是一个巧合?
先谢谢你

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题