1

I am new to Java.I have a function where I want the function to execute a multithreaded behaviour.The problem is that I will be making the jar without main method inside it.. Just wanted to know that can we have a multithreaded function in Java without a class having main method ?? I have the following code and I want this "myHandler" function to have multithreaded behaviour such that whenever this function gets called,different threads execute it...Can you please help me this code executing multithreaded behaviour?? Thank You

public String myHandler(KinesisEvent kinesisEvent,Context context)
{
    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");
     return null;
}
Akki
  • 493
  • 1
  • 11
  • 23

2 Answers2

1

If method should always be executed in separate thread, you can create a Thread, and call your code from that thread by following way:

    public String myHandler(final KinesisEvent kinesisEvent, final Context context) {
        new Thread(new Runnable() {
            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");
                return null;
            }
        }).start();
    }
Eldar Budagov
  • 312
  • 2
  • 11
  • Thank You for helping me with the solution...I tried all the approaches of multi-threading..But I want to know that whenever I execute the code as single-threaded it works much faster whereas whenever I make that code as multi-threaded it gives me slow performance...What can I do so that whenever I execute code with multithreaded approach,it works faster ?? – Akki Dec 09 '15 at 05:25
1

I'm not sure if that is exactly what you want but you could do something like this:

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();
}

This code will start your code in a new thread once you call the method but any parameters you want to use in the thread have to be declared final to be visible in the anonymous implementation of Runnable.

Another solution would be to create a new class and extend the Thread class:

public class MyHandlerThread extends Thread {
    KinesisEvent kinesisEvent;
    Context context;

    public MyHandlerThread(KinesisEvent kinesisEvent, Context context) {
        super();
        this.kinesisEvent = kinesisEvent;
        this.context = context;
    }

    @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");
    }

}

In order to start this as a thread you have to create an instance of the object and call its start method.

MyHandlerThread thread = new MyHandlerThread(param1, param2);
thread.start();

Hope this helps (:

scsere
  • 621
  • 2
  • 15
  • 25
  • Thank You for helping me with the solution...I tried all the approaches of multi-threading..But I want to know that whenever I execute the code as single-threaded it works much faster whereas whenever I make that code as multi-threaded it gives me slow performance...What can I do so that whenever I execute code with multithreaded approach,it works faster ?? – Akki Dec 09 '15 at 05:25
  • @Akki Well... performance improvements using threads depend on a lot of things. For example on what kind of CPU your code runs and how many threads you are using. Maybe this can help you a little: [Performance with multiple threads](http://stackoverflow.com/questions/15292626/does-multi-threading-improve-performance-how) – scsere Dec 09 '15 at 18:26
  • Thanks for sharing the information...that was quite useful & understandable..Thanks a lot – Akki Dec 10 '15 at 05:41