簡體   English   中英

AWS Lambda Java多線程

[英]AWS Lambda Java Multithreading

您好我為Kinesis流制作了一個AWS Lambda函數,批量大小為100,我試圖在多線程環境中執行它,但問題是在多線程環境中,與單線程相比,它的工作速度非常慢。就像我可以分享你的數字:對於512 MB,60秒超時,Lambda函數在684 ms內執行955條記錄,在435 ms內執行1031條記錄。對於多線程,它在878808 ms中執行430條記錄,在893862 ms內執行相同內存的433條記錄(即512 MB和60秒超時)

以下是我的Lambda函數代碼,它以多線程行為執行:

public String myHandler(final KinesisEvent kinesisEvent, final Context context) {
      Thread thread = new Thread(new Runnable(){

            //@Override
            public void run() {
                int singleRecord=0;
                long starttime=System.currentTimeMillis();
                //LambdaLogger lambdaLogger=context.getLogger();
                for(KinesisEventRecord rec : kinesisEvent.getRecords())
                {
                    singleRecord=0;
                    System.out.println("Kinesis Record inside is:"+new String(rec.getKinesis().getData().array()));
                    //count++;
                    singleRecord++;
                    //  System.out.println(new String(rec.getKinesis().getData().array()));
                }
                count=count+singleRecord;
                long endtime=System.currentTimeMillis();
                long totaltime = endtime-starttime;
                time=time+totaltime;
                System.out.println("Time required to execute single Lambda function for "+singleRecord+" records is"+" :: "+totaltime+" milliseconds");
                System.out.println("Total time required to execute Lambda function for "+count+" records is"+" :: "+time+" milliseconds");
            }
        });
        thread.start();
        return null;
    } //end of handler method

Lambda函數在多線程環境中執行速度慢嗎? 我想知道這個多線程Lambda函數處理速度慢的原因是什么? 如果我希望這個函數比單線程函數更快地工作,那么我應該在這段代碼中做些什么改變?

另外兩個選擇:

  1. 讓您的初始Lambda使用SDK異步啟動實際工作的其他lambda(扇出)
  2. 將Lambda調度與調度偏移和長時間超時配合使用,以便同時運行多個lambda。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM