0

Below is my sample implementation code, where code running without proper threading takes less time than code running with proper threading implementation, Kindly advice what mistake I am making or issues in my code

   @Override
   public Integer timeTakenWithoutThreading() {
       System.out.println("StartTime" + new Date());
       Date startDate = new Date();
       int value = 1000000;
       int value2 = 2000000;
       for (int i = 0; i <= value; i++) {
           System.out.println("Value 1" + i);
       }
       for (int i = 0; i <= value2; i++) {
           System.out.println("Value 2" + i);
       }
       System.out.println("StartTime" + startDate);
       System.out.println("endTime" + new Date());
       return null;
   }

   @Override
   public String timeTakenWithThreading() {
       Date start = new Date();
       Date firstThread = new Date();
       threadPoolExecutorService.execute(new Runnable() {
           final int value = 1000000;

           @Override
           public void run() {
               for (int i = 0; i <= value; i++) {
                   System.out.println("Thread 1 Value" + i);
               }

           }
       });

       threadPoolExecutorService.execute(new Runnable() {
           final int value2 = 2000000;

           @Override
           public void run() {
               for (int i = 0; i <= value2; i++) {
                   System.out.println("Thread 2 Value" + i);
               }
               System.out.println("Start Date" + start);
               System.out.println("End Date" + new Date());
           }
       });

       return "You Will Get Response So Soon But Thread Will Execute At the Background";
   }
}```
  • 1
    I think for measurements you do arithmetic operations instead of printing to console. System.out is a PrintStream which is a synchronized class. So multiple threads writing large amounts to System.out will block each other. https://stackoverflow.com/questions/17304013/is-console-output-a-blocking-operation#:~:text=out%20will%20block%20each%20other,then%20the%20write%20will%20block. – the Hutt Mar 14 '21 at 06:19
  • By the way, `Date` class is terrible, and obsolete. Supplanted years ago by `java.time.Instant`. And, use `Duration` to calculate elapsed time. – Basil Bourque Mar 14 '21 at 08:10
  • What exactly do you mean by « proper threading implementation »? – Basil Bourque Mar 14 '21 at 08:15
  • Dear @BasilBourque , First implemented method will print the data based on iteration and both will value and value 2 will print by a single thread. But my second implemented method will execute both iterations in parallel and i expect some performance improvements. that is what i mean by proper threading implementation. – vetri selvan Mar 14 '21 at 13:42

1 Answers1

0

You count the start time even when the parallel execution is not started. So the time measurement is actually wrong. You should initialize the start time inside the Runnable.

for example:

       
       threadPoolExecutorService.execute(new Runnable() {
           
           final int value = 1000000;

           @Override
           public void run() {
               // time started.
               Date firstThread = new Date();
               System.out.println("First thread started at" + firstThread);
               for (int i = 0; i <= value; i++) {
                   System.out.println("Thread 1 Value" + i);
               }
               // time ended.
               System.out.println("First thread ended at" + new Date());
           }
       });

Also apply this for the second thread. You'll see that both thread many run in parallel. And in combined it would take less time than the non-threaded one.

MD Ruhul Amin
  • 4,386
  • 1
  • 22
  • 37