3.MULTITHREADING IN JAVA

  3.MULTITHREADING IN JAVA Source Code:





 class SquareThread extends Thread { public void run(){ try { for(int i=1;i<=5;i++) { System.out.println("Square of " +i +" is" +(i*i)); Thread.sleep(1000); } } catch(Exception e) { System.out.println(e); } } } class CubeThread extends Thread{ public void run(){ try { for(int i=1;i<=5;i++) { System.out.println("Cube of " +i +" is" +(i*i*i)); } } catch(Exception e) { System.out.println(e); } } } class SquareRootThread extends Thread{ public void run(){ try { for(int i=1;i<=5;i++) { System.out.println("Square Root of " +i +" is" +(i*0.5)); Thread.sleep(10); } } catch(Exception e) { System.out.println(e); } } } class EmptyThread extends Thread{ public void run(){ for(int i=1;i<=5;i++) { System.out.println("Hello Thread"); } } } public class ThreadExample { public static void main(String[] args) { System.out.println("Multithreading in Java"); System.out.println("**********************"); SquareThread t1 =new SquareThread(); CubeThread t2=new CubeThread(); SquareRootThread t3=new SquareRootThread(); EmptyThread t4=new EmptyThread(); t1.start(); t2.start(); t3.start(); t4.start(); } } Output Multithreading in Java ********************** Square of 1 is1 Cube of 1 is1 Cube of 2 is8 Cube of 3 is27 Cube of 4 is64 Cube of 5 is125 Hello Thread Hello Thread Hello Thread Hello Thread Hello Thread Square Root of 1 is0.5 Square Root of 2 is1.0 Square Root of 3 is1.5 Square Root of 4 is2.0 Square Root of 5 is2.5 Square of 2 is4 Square of 3 is9 Square of 4 is16 Square of 5 is25

Comments