mardi 6 octobre 2020

Threads behaving in the opposite way of what I though to be

Just importing random numbers and counting no. of negatives , even_positive , even_negative ; Task is to see multithreading work reducing execution time; By) performing same task in main function and by multithreading;

Code 1)
In first I create a single class implementing Runnable. That single class does all the counting job. But according to me each function (talking about function call inside run() method) is executing when previous function ends and hence should even increase the time since we are looping 3 times to get the same data. But to my surprise the execution time is less than simply counting through one loop in main function;

Code 2)

In second case I created 3 class implementing Runnable interface so that all my process can be concurrent and hence should have reduced time. But to my surprise opposite happens. It takes more time than the single loop in main function for the same task;

import java.util.Random;

class ThreadClass implements Runnable {
    private int[] random;

    public ThreadClass(int[] a) {
        random=a;
    }

    int neg(int[] random) {
        int nega=0;
        for(int i=0;i<10000;i++) {
            if (random[i] < 0)
                nega++;
        }
        return nega;
    }

    int posiEven(int[] random) {
        int posEven=0;
        for(int i=0;i<10000;i++) {
            if (random[i] > 0){
                if(random[i]%2==0)
                    posEven++;
            }
        }
        return posEven;
    }

    int posiOdd(int[] random) {
        int posOdd=0;
        for(int i=0;i<10000;i++) {
            if (random[i] > 0){
                if(random[i]%2!=0)
                    posOdd++;
            }
        }
        return posOdd;
    }

    @Override
    public void run() {
        System.out.println("negative, positive odd, positive even "+neg(random)+" "+posiOdd(random)+" 
"+posiEven(random));
    }
}   

public class Lab6_Q2 {
    public static void main(String[] args) {
        int[] random = new int[10000];
        Random r = new Random();
        for(int i=0;i<10000;i++){
            random[i]=r.nextInt(201)-100;
        }
        int neg=0,posEven=0,posOdd=0;
        long startTime1 = System.nanoTime();
        for(int i=0;i<10000;i++){
            if(random[i]<0)
                neg++;
            else{
                if(random[i]%2==0)
                    posEven++;
                else
                    posOdd++;
            }
        }
        long endTime1 = System.nanoTime();
        System.out.println("negative, positive odd, positive even "+neg+" "+posOdd+" "+posEven);
        System.out.println("Time taken without using multiple threads: "+(endTime1-startTime1)+" ns");

        ThreadClass threadClass = new ThreadClass(random);
        Thread t1 = new Thread(threadClass);
        long startTime2 = System.nanoTime();
        t1.start();
        long endTime2 = System.nanoTime();

        System.out.println("Time taken  using multiple threads: "+(endTime2-startTime2)+" ns");
    }
}

Code 2)

import java.util.Random;

class Negative implements Runnable
{
private int[] random;
int neg;
public Negative(int[] a)
{
    random=a;
    neg= 0;
}
public void run()
{
    
    for(int i=0;i<10;i++) 
    {
        System.out.println("in_negative");
        if (random[i] < 0)
            neg++;
    }
}
}

class Positive_even implements Runnable
{
    private int random[];
    int pos_even;

public Positive_even(int a[])
{
    random = a;
    pos_even = 0;
    
}
    public void run()

        {
          for(int i=0;i<10;i++) 
          {
              System.out.println("in_pos_even");
                if (random[i] > 0)
                {
                if(random[i]%2==0)
                    pos_even++;
            }
         }
    }
}

class positive_odd implements Runnable
{
    private int random[];
    int pos_odd;

    public positive_odd(int a[])
{
    random = a;
    pos_odd = 0;
}
public void run()
{
      for(int i=0;i<10;i++) 
      {
          System.out.println("in_pos_odd");
            if (random[i] > 0)
            {
                if(random[i]%2 != 0)
                    pos_odd++;
            }
        }
}                                         
}
public class Lab6_Ques2
{
    public static void main(String[] args) 
    {
        int[] random = new int[10];
        Random r = new Random();
        for(int i=0;i<10;i++)
        {
            random[i]=r.nextInt(20)-10;
         }
    int neg=0,posEven=0,posOdd=0;
    long startTime1 = System.nanoTime();
    for(int i=0;i<10;i++){
        if(random[i]<0)
            neg++;
        else{
            if(random[i]%2==0)
                posEven++;
            else
                posOdd++;
        }
    }
    long endTime1 = System.nanoTime();
    System.out.println("negative, positive odd, positive even "+neg+" "+posOdd+" "+posEven);
    System.out.println("Time taken without using multiple threads: "+(endTime1-startTime1)+" ns");
                                                   
    Negative threadClass1 = new Negative(random); 
    Thread t1 = new Thread(threadClass1); 
     
    Positive_even threadClass2 = new Positive_even(random);
    Thread t2 = new Thread(threadClass2);
    
    positive_odd threadClass3 = new positive_odd(random);
    Thread t3 = new Thread(threadClass3);
    
    long startTime2 = System.nanoTime(); 
    t1.start();
    t2.start();
    t3.start();
    long endTime2 = System.nanoTime(); 
                                                                         
    System.out.println("Time taken  using multiple threads: "+(endTime2-startTime2)+" ns");
}
}



Aucun commentaire:

Enregistrer un commentaire