Wednesday, 27 June 2018

Multithreading solution


QUESTION-Let us consider the processing of the following mathematical equation:
p = sin (x) + cos (y) + tan (z)
As these trigonometric functions are independent operations without any dependencies between them, they can be executed concurrently. After that their results can be combined to produce the final result.
Develop a multithreaded application which calculates these trigonometric functions using 3 threads.
For calculating sin( ) , cos( ) and tan( ) you can use the Math class.


make 4 classes-
by names -

sin-
import static java.lang.Thread.sleep;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Naman Khurpia
 */
public class sin implements Runnable{

    Thread th;
    float value;
    public sin(float val)
    {
        th=new Thread(this);
        th.start();
        value=val;
    }
    
    @Override
    public void run() {
        
                value=(float) Math.sin(Math.toRadians(value));
                System.out.println("Value of sin is "+value); 
        try {
            sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(sin.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    
}

_________________________________________________________________________________

COS-

import static java.lang.Thread.sleep;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Naman Khurpia
 */
public class cos implements Runnable{
    Thread th;
    float value;
    public cos(float val)
    {
        th=new Thread(this);
        th.start();
        value=val;
    }
    
    @Override
    public void run() {
        
                value=(float) Math.cos(Math.toRadians(value));
                System.out.println("Value of cos is "+value); 
        try {
            sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(sin.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    
}


_________________________________________________________________________________

tan-

import static java.lang.Thread.sleep;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Naman Khurpia
 */
public class tan implements Runnable{
    Thread th;
    float value;
    public tan(float val)
    {
        th=new Thread(this);
        th.start();
        value=val;
    }
    
    @Override
    public void run() {
        
                value=(float) Math.tan(Math.toRadians(value));
                System.out.println("Value of tan is "+value); 
        try {
            sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(sin.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    
}


_________________________________________________________________________________

main class-

import assignment_five_multithreading_trignometric_funtions.main;
import java.util.Scanner;

/**
 *
 * @author Naman Khurpia
 */
public class mainclass {
    public static void main(String[] args) {
        
        Scanner kb=new Scanner(System.in);
        System.out.println("Enter value of x in degrees");
        float x=kb.nextFloat();
        System.out.println("Enter value of y in degrees");
        float y=kb.nextFloat();
        System.out.println("Enter value of z in degrees");
        float z=kb.nextFloat();
        
        sin s=new sin(x);
        cos c=new cos(y);
        tan t=new tan(z);
        
        try
        {
        s.th.join();
        c.th.join();
        t.th.join();
        
        }
        catch(InterruptedException ex)
        {
            System.out.println("Interrupt 3caught in main");
        }
  
        float value=(s.value+c.value+t.value);
        System.out.println("Final value is "+value);
        System.out.println("main finish");
    }
    
}


_________________________________________________________________________________


THE END



1 comment:

GPT4ALL - A new LLaMa (Large Language Model)

posted 29th March, 2023 - 11:50, GPT4ALL launched 1 hr ago  What if we use AI generated prompt and response to train another AI - Exactly th...