多線程獲取返回值

1.共享變量

將要保存/修改的值set到共享變量中,通過(guò)獲取共享變量實(shí)現(xiàn)值在不同線程中的傳遞

如下在子線程中循環(huán)累加學(xué)生年齡,然后在主線程中獲取累加后的結(jié)果

    //共享變量
    private static AtomicReference<Student> student = new AtomicReference<>();
    private static volatile boolean die = false;

    static void updateStudent() {
        student.updateAndGet(stu -> {
            stu.setAge(stu.getAge() + 1);
            return stu;
        });
    }

    public static void main(String[] args) {
        Student stu = new Student();
        stu.setAge(10);
        student.set(stu);

        new Thread(() -> {
            for (int i = 0; i < 90; i++) {
                updateStudent();
            }
            die = true;
        }).start();

        while (true) {
            if (die) {
                System.out.println("年齡:" + student.get().getAge());
                break;
            }
        }
    }

2.FutureTask

先看代碼

        FutureTask<Student> futureTask = new FutureTask<>(new Callable<Student>() {
            @Override
            public Student call() throws Exception {
                Student student = new Student();
                student.setName("zhangSan");
                student.setAge(10);
                student.setSex("man");
                Thread.sleep(1000);
                return student;
            }
        });
        new Thread(futureTask).start();

        while (true) {
            if (futureTask.isDone()) {
                Student student = futureTask.get();
                System.out.println(student);
                break;
            }
        }

new Thread(futureTask).start() 可以看出FutureTask 一定實(shí)現(xiàn)了Runnable接口, 創(chuàng)建FutureTask 需要一個(gè)Callable 參數(shù),Callable 是一個(gè)Function接口只有一個(gè)call方法,我們的業(yè)務(wù)代碼寫在call 方法里,所以調(diào)用邏輯應(yīng)該是

thread.start() -> futureTask.run() -> callable.call()

線程啟動(dòng),調(diào)用run方法,run方法內(nèi)部調(diào)用call 方法

Futuretask的run方法部分代碼

......
        try {
            Callable<V> c = callable;
            if (c != null && state == NEW) {
                V result;
                boolean ran;
                try {
                    //調(diào)用callable.call()
                    result = c.call();
                    ran = true;
                } catch (Throwable ex) {
                    result = null;
                    ran = false;
                    //設(shè)置異常信息,狀態(tài)等
                    setException(ex);
                }
                if (ran)
                    //設(shè)置返回值,狀態(tài)等
                    set(result);
            }
        }
......
Future

Futuretask 除了實(shí)現(xiàn)Runable還實(shí)現(xiàn)了Future接口,future有如下接口

  • boolean cancel(boolean mayInterruptIfRunning); 嘗試取消執(zhí)行這個(gè)任務(wù),如果線程已經(jīng)執(zhí)行完畢或者已經(jīng)取消會(huì)取消失敗,如果任務(wù)已經(jīng)開(kāi)始,則根據(jù)mayInterruptIfRunning執(zhí)行是否取消,需要注意的是這里的取消相當(dāng)于thread.interrupt(),并不能停止任務(wù),當(dāng)然如果任務(wù)還沒(méi)有開(kāi)始執(zhí)行那么取消是成功的.

  • boolean isCancelled(); 任務(wù)是否被取消

  • boolean isDone(); 任務(wù)是否執(zhí)行完成,任務(wù)正常執(zhí)行完,被取消或拋出異常都會(huì)返回true

  • V get() ; 獲取返回值,需要注意的是這是一個(gè)阻塞的方法,會(huì)拋InterruptedException

  • V get(long timeout, TimeUnit unit) 與get方法一樣,可以設(shè)置等待時(shí)間,超時(shí)會(huì)拋TimeoutException

3.線程池-ThreadPool

向線程池提交線程會(huì)返回一個(gè)上面說(shuō)的Future對(duì)象,通過(guò)Future獲取返回值

    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 5, 5L, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(5),
            new ThreadFactory() {
                @Override
                public Thread newThread(Runnable r) {
                    Thread thread = new Thread(r);
                    thread.setDaemon(true);
                    return thread;
                }
            });

   //提交任務(wù),獲得Future
    Future<Integer> submit = threadPoolExecutor.submit(new Callable<Integer>() {
        @Override
        public Integer call() throws Exception {
            return 100;
        }
    });

    while (true) {
        if (submit.isDone()) {
            System.out.println(submit.get());
            break;
        }
    }

轉(zhuǎn)載請(qǐng)注明出處

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容