java 同步調用和異步調用



同步和異步:


同步和異步通常用來形容一次方法的調用。

同步方法的調用必須等到該方法返回后才能繼續接下來的行為。

異步方法更像一個消息傳遞,一旦調用就會立即返回,調用者可以繼續接下來的操作,

而異步方法通常會在另一個線程中執行,不會妨礙調用者的工作。

示例代碼:
package com.infinitePossibilities.utils;
import java.util.concurrent.*;


public class TbOrYb {

    public static void main(String[] args) throws Exception {

        TbOrYb tbOrYb = new TbOrYb();
        tbOrYb.tb();
        Thread.sleep(2000);
        System.out.println("\n");
        tbOrYb.yb();
        Thread.sleep(2000);
        System.out.println("\n");
        tbOrYb.yb2();
    }

    //同步
    public void tb() throws Exception{

        TbOrYb tbOrYb = new TbOrYb();
        long start = System.currentTimeMillis();
        tbOrYb.api1("tb");
        tbOrYb.api2("tb");
        long end = System.currentTimeMillis();
        System.out.println("同步用時:"+(start-end));

    }

    //異步,不要返回結果
    public void yb() throws Exception{

        TbOrYb tbOrYb = new TbOrYb();
        long start = System.currentTimeMillis();

        new Thread(() -> {
            try {
                TbOrYb.api1("yb");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();

        tbOrYb.api2("yb");
        long end = System.currentTimeMillis();
        System.out.println("異步 1 用時:"+(start-end));
    }


    //異步,要返回結果
    public void yb2() throws Exception {

        TbOrYb tbOrYb = new TbOrYb();
        long start = System.currentTimeMillis();


        Callable<String> callable = new Callable<String>() {
            @Override
            public String call() throws Exception {
                TbOrYb.api1("yb2");
                return "111";
            }
        };
        Future <String> future =  threadPoolExecutor.submit(callable);//異步調用執行方法

        tbOrYb.api2("yb2");

        //這里會阻塞,需要得到返回的結果
        String data = future.get();

        long end = System.currentTimeMillis();
        System.out.println("異步 2 用時:"+(start-end)+" 結果:"+data);
        threadPoolExecutor.shutdown();
    }



    public static void api1(String flag) throws Exception {

        Thread.sleep(3000);
        System.out.println(flag+" 調用接口 1 完畢!");
    }

    public static void  api2(String flag) throws Exception{

        Thread.sleep(2000);
        System.out.println(flag+" 調用接口 2 完畢!");

    }


    //自定義線程池
    private ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                                                    1,3,
                                                    60, TimeUnit.SECONDS,
                                                    new ArrayBlockingQueue<>(5),
                                                    Executors.defaultThreadFactory(),
                                                    //自定義拒絕策略
                                                    new MyRejectedExecutionHandler()
                                                    );

}

 

運行結果:

 

 

自定義拒絕策略:

package com.infinitePossibilities.utils.threads.thresdPool_MQ_Impl;

import java.util.Random;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 自定義線程池拒絕策略
 */
public class MyRejectedExecutionHandler implements RejectedExecutionHandler {
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
//拒絕掉的任務 開啟新線程去執行
        new Thread(r,"新啟線程"+new Random().nextInt(10)).start();
    }
}
 
        

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM